如何在分割字符串后获取第一行。 所以我有一个文本文档,文本文档的内部是...... (测试结果) 我想打印出“结果”。
public static void main(String[] args)
{
System.out.println("Reading File from Java code");
//Name of the file
String fileName="C:\\Users\\Olive\\Desktop\\accounts.txt";
try{
//Create object of FileReader
FileReader inputFile = new FileReader(fileName);
//Instantiate the BufferedReader Class
BufferedReader bufferReader = new BufferedReader(inputFile);
//Variable to hold the one line data
String line =" ";
String result = line.trim();
// Read file line by line and print on the console
while ((line = bufferReader.readLine()) != null) {
System.out.println(result.trim());
}
//Close the buffer reader
bufferReader.close();
}catch(Exception e){
System.out.println("Error while reading file line by line:" + e.getMessage());
}
答案 0 :(得分:0)
尝试类似:
String mystr = "test:result".split(":")[1];
或者使用子字符串,如:
String str = "test:result";
String mystr = str.substring(str.lastIndexOf(":") + 1);