我对这项技术还不熟悉。我有一个名为condition.txt的文本文件,内容如下:
server_state running
health_state ok
heappercent 20%
hoggingthreadcount 10
stuckthreadcount 1
我有一个Java文件,现在想要从文件中读取这些数据并访问这些值。比如说,在我的Java文件中,我有一个名为server_state的字符串变量。我想将值“running”放入我的字符串变量中。该怎么做呢?
我对这项技术还不熟悉。我有一个名为condition.txt的文本文件,内容如下:
server_state running
health_state ok
heappercent 20%
hoggingthreadcount 10
stuckthreadcount 1
我有一个Java文件,现在想要从文件中读取这些数据并访问这些值。比如说,在我的Java文件中,我有一个名为server_state的字符串变量。我想将值“running”放入我的字符串变量中。该怎么做呢?
看起来你想从文件中读取内容?在这种情况下,Scanner类非常适合使用。
你需要以不同的方式格式化文本文档,这意味着如果你是通过编程方式写入文件,则需要以某种方式拆分这两个变量。我认为逗号(,)或冒号(:)是最容易选择的方式。
stuckthreadcount:1
public void getProps() {
String[] cur = new String[2];
Scanner scanner = new Scanner("C:\Path/To/Your/File.txt");
while(scanner.hasNextLine()) {
cur = scanner.nextLine().split(":"); // a colon is simpler.
if(cur[0].equalsIgnoreCase("server_state")) {
server_state = cur[1];
}
if(cur[0].equalsIgnoreCase("health_state")) {
health_state = cur[1];
}
if(cur[0].equalsIgnoreCase("heappercent")) {
heappercent = Double.parseDouble(cur[1]);
}
if(cur[0].equalsIgnoreCase("hoggingthreadcount")) {
hoggingthreadcount = Integer.parseInt(cur[1]);
}
if(cur[0].equalsIgnoreCase("stuckthreadcount")) {
stuckthreadcount = Integer.parseInt(cur[1]);
}
}
scanner.close();
}
public static Countries[] readObjects() throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("d:\\countries.txt"));
String line = "";
Countries[] countriesArray = new Countries[32];
int arrayIndex = 0;
/***
* Read File line by line and parse every line to Countries object and insert it into the array
*/
while ((line = br.readLine()) != null) {
/***
* Create Country object from the line
*/
Countries country = createCountryObect(line);
countriesArray[arrayIndex] = country;
arrayIndex++;
}
br.close();
return countriesArray;
}
/***
* Read string and parse it into an object
* @param line from the file
* @return Countries Object
*/
public static Countries createCountryObect(String line)
{
Countries country = new Countries();
StringTokenizer st = new StringTokenizer(line," ");
while(st.hasMoreElements())
{
country.countryName = st.nextElement().toString();
//convert country population from string to integer
country.countryPopulation = Integer.parseInt(st.nextElement().toString());
}
return country;
}
以及国家类
public class Countries {
public String countryName;
public int countryPopulation;
}
这是我在工作中需要编写的类似程序:
public class LogReader {
public static void main(String[] args) throws IOException
{
mapper("workId=\"1915900093138425722");
}
public static void mapper(String id) throws IOException
{
String sepText = "";
FileWriter fileWriter;
BufferedReader br = new BufferedReader(new FileReader(new File("Work.txt")));
String line = "";
while ((line = br.readLine()) != null)
{
System.out.print(line);
sepText += line.substring(0, 23) + "\n";
int startAttIdMarker = line.indexOf(id);
int midAttIdMarker = line.indexOf('"', startAttIdMarker + 1);
int endAttIdMarker = line.indexOf('"', midAttIdMarker + 1 );
sepText += "attributeSetId = " + line.substring(midAttIdMarker + 1 , endAttIdMarker) + "\n" + "\n";
sepText += "********************************************" + "\n";
}
br.close();
File newTextFile = new File("worker.txt");
fileWriter = new FileWriter(newTextFile);
fileWriter.write(sepText);
fileWriter.close();
}
}