2016-11-22 52 views
1

问题:我无法用空格解析我的文件test.txt。我可以1)读取文本文件,并且我可以2)解析字符串,但是我无法连接这两个文件并解析文本文件!我的目的是学习如何分析文本文件。这是一个简化的方法。合并读取和解析文本text.txt

进度:到目前为止,我可以使用FileReader和BufferedReader读取test.txt,并将其打印到控制台。此外,我可以解析简单的字符串变量。单个操作运行,但我正在努力解析一个实际的文本文件。我相信这是因为我的test.txt存储在缓冲区中,并且在.close()之后,我无法打印它。

文本文件的内容: 这是创建 文本文件,只 用于测试目的。

代码:

import java.io.*; 

public class ReadFile { 

    //create method to split text file, call this from main 
    public void splitIt(String toTest) 
    { 
     String[] result = toTest.split(" "); 

     for (String piece:result) 
     { 
      //loop through the array and print each piece 
      System.out.print(piece); 
     } 
    } 


    public static void main(String[] args) { 

     //create readfile method  
     try 
     { 
      File test = new File("C:\\final\\test.txt"); 
      FileReader fileReader = new FileReader(test); 
      BufferedReader reader = new BufferedReader(fileReader); 

      String line = null; 

      //While there are still lines to be read, read and print them 
      while((line = reader.readLine()) != null) 
      { 
       System.out.println(line); 
       splitIt(line); 
      } 

      reader.close(); 
     } 

     //Catch those errors! 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 

//  readFileMethod a = new readFileMethod(line); 
     System.out.println(a.splitIt()); 

    } 

} 

抢占感谢您的分享你的知识。阅读和解析的许多帖子都已经在这里解决了,但我没有理解实现他人的解决方案。请原谅,我只是在几个月的时间里一直在学习Java,并仍然在与基础知识斗争。

+0

你能解释一下你试图实现的内容吗? – root

+0

嘿根,我已经添加了文本文件内容(只是简单的占位符文本),并描述了我的目的。我想让文本文档中的每个单词用行分隔。 – PizzaAndCode

回答

0

大厦可怕的袋熊和你的代码,我做了一些修改调用它。 它现在应该打印正在读入的行和每个由空格分隔的单词。

import java.io.*; 

public class ReadFile { 

    //create method to split text file, call this from main 
    public static void splitIt(String toTest) 
    { 
     String[] result = toTest.split(" "); 

     for (String piece:result) 
     { 
      //loop through the array and print each piece 
      System.out.println(piece); 

     } 
    } 


    public static void main(String[] args) { 

     //create readfile method 
     try 
     { 
      File test = new File("C:\\final\\test.txt"); 
      FileReader fileReader = new FileReader(test); 
      BufferedReader reader = new BufferedReader(fileReader); 

      String line = null; 

      //While there are still lines to be read, read and print them 
      while((line = reader.readLine()) != null) 
      { 
       System.out.println(line); // print the current line 
       splitIt(line); 
      } 

      reader.close(); 
     } 

     //Catch those errors! 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 

    } 

} 
+0

这工作完美无瑕,我感谢完整的工作代码:) – PizzaAndCode

+1

@ ScaryWombat erm ...我真的很欣赏你的答案。为什么要提供完整的解决方案downvote root?关于讽刺性的学习评论,自从发布这个问题以来,我已经阅读了几百页Java,并大大增加了我的理解。因此,我从头开始重写了这些代码几次。我不知道你为什么会对曾经和你站在同一个鞋子里的人采取如此便宜的枪击......但这并不重要。尽管如此,我很欣赏这个学习者和老师的社区。 – PizzaAndCode

3

确定可以使分裂成mthod

private static void splitIt (String toTest) { 

    String[] result = toTest.split(" "); 

    for (String piece:result) 
    { 
     //loop through the array and print each piece. 
     System.out.println(piece); 
    } 
} 

那么你可以从内部

while((line = reader.readLine()) != null) 
    { 
     System.out.println(line); 
     splitIt (line); 
    } 
+0

嗨吓人的袋熊,我*试图*将您的答案纳入我的代码块,但我仍然受到事实,我从静态主要内部调用非静态(我相信)的阻碍。几小时的工作和30页的阅读没有产生任何解决方案,但很明显,这是一个常见的初学者问题:(你有任何线索? – PizzaAndCode

+0

@PizzaAndCode由于'splitIt'不是一个静态方法,它需要它包含的类首先被实例化(创建一个新的类对象),你可以在main中创建一个新的ReadFile对象,然后从该对象本身调用该方法,但它可能是最简单的case)更有意义)只是使'splitIt'成为一个静态方法 – kunruh

+0

@kunruh感谢您的评论我编辑了我的答案,使这种方法'静态' –