【问题标题】:reading from file error java从文件错误java读取
【发布时间】:2015-03-01 18:14:28
【问题描述】:

我使用下面的代码从 txt 文件中读取。

ArrayList<String> elements = new ArrayList<String>();
String line = null;
BufferedReader in = new BufferedReader(new FileReader("example.txt"));
//in.close();
while((line = in.readLine()) != null){
    elements.add(line);
}
check1 = (elements.get( 0));
System.out.println("HEYAAA \n" + check1);
//GUIServer guiServer = new GUIServer();
}catch(Exception e){
    System.out.println("Error while reading file line by line:" + e.getMessage());
}

我有“1. 我的名字是 bob \n 2. 帮助你 \n 3. 只是测试”在 txt 文件中全部在一行中。但是当我将它读入 java 时,它全部打印在一行中并且没有't read "\n".. 我该怎么做才能打印出 3 行,所以它会读取 "\n" ..?

或者,我可以在 txt 文件中手动将它们分开,每个新行,但是我如何读取例如 3 行并存储到一个变量中..?

哪一种方法更容易,我将不胜感激。

【问题讨论】:

  • 文本文件中的 \n 只会被 java 读取为文本,因此将其拆分为单独的行将是最简单的解决方案。
  • 但是您如何读取 3 行并将其存储到一个变量中.. 然后再为接下来的 3 行将其存储在另一个变量中..
  • 这个问题看起来像XY problem。请说明您想要实现的目标以及您的数据是什么样的,因为\n 似乎是您尝试解决实际问题的结果,而不是您想要的输入格式。
  • 您需要在 while 循环中使用计数器,并且每次通过循环时,您都会将字符串添加到 StringBuilder,当 counter%3 == 0 时,您会将 StringBuilder 添加到列表中,然后重置 StringBuilder。然后当循环结束时,如果 StringBuilder 不为空,则将其添加到列表中。
  • 顺便说一句,由于您似乎是 Stack Overflow 上的新手,您可能不知道要纠正您的问题,您可以使用帖子下方的 [edit] 选项。

标签: java java-io


【解决方案1】:

1) “example.txt”包含:"1. my name is bob \n 2. help you \n 3. Just testing"

2) 你想把它解释为

1. my name is bob
2. help you
3. Just testing

3) 缓冲阅读器不会自动转换 "\n" - 您必须在程序中执行此操作。

4) 建议:

String.split()String.replace()

附录 - 这是一个完整的示例:

1) 代码:

package com.so.readlines;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;

public class ReadLines {

    public static void main(String[] args) {
        // Declare our array list
        ArrayList<String> elements = new ArrayList<String>();
        String line;
        BufferedReader in = null;
        try {
            // Open file
            in = new BufferedReader(new FileReader("example.txt"));
            // Read next line (with embedded "\n" text string)
            while((line = in.readLine()) != null){
                // ... split "line" into String[] array; add it to "elements" arraylist
                // Collections.addAll(elements, line.split("\\\\n"));
                String[] a = line.split("\\\\n");
                Collections.addAll(elements, a);
            }
            in.close();
            //String check1 = (elements.get( 0));
            //System.out.println("HEYAAA \n" + check1);
            //GUIServer guiServer = new GUIServer();
        }
        catch(Exception e){
            System.out.println("Error while reading file line by line:" + e.getMessage());
        }

        // Print contents of array list
        int i = 0;
        for (String s : elements) {
            System.out.println ("elements[" + i++ + "]=" + s);
        }
    }

}

2) Example.txt(注意“\n”后面的多余空格):

1. my name is bob \n 2. help you \n 3. Just testing

3) 示例输出:

elements[0]=1. my name is bob 
elements[1]= 2. help you 
elements[2]= 3. Just testing

'希望对您有所帮助......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多