【问题标题】:Read a text file line by line into strings将文本文件逐行读入字符串
【发布时间】:2015-12-10 18:16:43
【问题描述】:

如何在不使用BufferedReader 的情况下将文本文件的内容逐行读入String

例如,我有一个内部看起来像这样的文本文件:

Purlplemonkeys
greenGorilla

我想创建两个strings,然后使用类似的东西

File file = new File(System.getProperty("user.dir") + "\Textfile.txt");
String str = new String(file.nextLine());
String str2 = new String(file.nextLine());

这样,它会将"Purlplemonkeys" 的值分配给str,并将"greenGorilla" 的值分配给str2

【问题讨论】:

    标签: java arrays string line


    【解决方案1】:

    您可以读取文本文件以列出:

    List<String> lst = Files.readAllLines(Paths.get("C:\\test.txt"));
    

    然后根据需要访问每一行

    附:文件 - java.nio.file.Files

    【讨论】:

      【解决方案2】:

      您应该使用ArrayList

      File file = new File(fileName);
      Scanner input = new Scanner(file);
      List<String> list = new ArrayList<String>();
      
      while (input.hasNextLine()) {
          list.add(input.nextLine());
      }
      

      然后您可以从其索引中访问列表中的一个特定元素,如下所示:

      System.out.println(list.get(0));
      

      这会给你第一行(即:Purlplemonkeys)

      【讨论】:

      • 你不需要知道它是一个 ArrayList。编码List&lt;String&gt; arrayList = new ArrayList&lt;String&gt;(); 更干净
      • 我正在使用 Eclipse。在arrayList.add(Input.nextLine()); 旁边有一个错误,上面写着Multiple markers at this line - Syntax error on token ")", ; expected - implements java.awt.event.ActionListener.actionPerformed - Syntax error on token "(", ; expected,在File file = new File(fileName); 旁边有一个错误,上面写着fileName cannot be resolved to a variable 这也会导致我班级其他部分的错误。
      • 另外,txt文件的路径应该放在哪里?
      • 您将文件路径放在fileName 所在的位置。像这样:File file = new File("ThisIsYourFilePath.txt");
      【解决方案3】:

      如果您使用 Java 7 或更高版本

      List<String> lines = Files.readAllLines(new File(fileName));
      
      for(String line : lines){
         // Do whatever you want
         System.out.println(line);
      }
      

      【讨论】:

      • 我个人最喜欢的方法:)
      • Files类型中的readAllLines(Path)方法不适用于参数(File)
      【解决方案4】:

      Sinse JDK 7 很容易将文件读入行:

      List<String> lines = Files.readAllLines(new File("text.txt").toPath())
      
      String p1 = lines.get(0);
      String p2 = lines.get(1);
      

      【讨论】:

        【解决方案5】:

        commons-io怎么样:

        List<String> lines = org.apache.commons.io.IOUtils.readLines(new FileReader(file));
        
        //Direct access if enough lines read
        if(lines.size() > 2) {
          String line1 = lines.get(0);
          String line2 = lines.get(1);
        }
        
        //Iterate over all lines
        for(String line : lines) {
          //Do something with lines
        }
        
        //Using Lambdas
        list.forEach(line -> {
          //Do something with line
        });
        

        【讨论】:

          【解决方案6】:

          您可以使用apache.commons.io.LineIterator

          LineIterator it = FileUtils.lineIterator(file, "UTF-8");
           try {
             while (it.hasNext()) {
               String line = it.nextLine();
               // do something with line
             }
           } finally {
             it.close();
           }
          

          也可以通过覆盖boolean isValidLine(String line) 方法来验证行。 refer doc

          【讨论】:

            【解决方案7】:
            File file = new File(fileName);
            Scanner input = new Scanner(file);
            while (input.hasNextLine()) {
              System.out.println(input.nextLine());
            }
            

            【讨论】:

            • 我认为这不是他想要的。 line 每次都会被覆盖,所以即使声明它也是毫无意义的。为什么不只是 println(input.nextLine())String line = ... 有点多余。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-07-30
            • 1970-01-01
            • 1970-01-01
            • 2017-09-12
            相关资源
            最近更新 更多