如何从文本文档中解析拆分字符串

时间:2015-03-08 09:13:24

标签: java

如何在分割字符串后获取第一行。 所以我有一个文本文档,文本文档的内部是...... (测试结果) 我想打印出“结果”。

    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());                      
   }

1 个答案:

答案 0 :(得分:0)

尝试类似:

String mystr = "test:result".split(":")[1];

或者使用子字符串,如:

String str = "test:result";
String mystr = str.substring(str.lastIndexOf(":") + 1);