Java文件读取问题

Java文件读取问题,java,file,execution,Java,File,Execution,我在尝试读取文件时遇到了一些非常棘手的问题 它只有几行简单的代码,但出于某种原因,它认为我的文件有8行wierd rumbo jumbo文本,而每行有2行和4个字母 代码(执行一次,读取正确的文件) 输出: 8 Size 它非常奇怪,因为它只有几行和一个简单的文件 有什么帮助、建议或错误吗?没有任何痕迹 谢谢 杰克我建议采用不同的方法,这样做更正确 public static void main(String[] args) { BufferedReader br = null;

我在尝试读取文件时遇到了一些非常棘手的问题

它只有几行简单的代码,但出于某种原因,它认为我的文件有8行wierd rumbo jumbo文本,而每行有2行和4个字母

代码(执行一次,读取正确的文件)

输出:

8 Size
它非常奇怪,因为它只有几行和一个简单的文件

有什么帮助、建议或错误吗?没有任何痕迹

谢谢


杰克

我建议采用不同的方法,这样做更正确

public static void main(String[] args) {

    BufferedReader br = null;
    ArrayList<String> lines = new ArrayList<String>();

    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader("C:\\testing.txt"));

        while ((sCurrentLine = br.readLine()) != null) {
            lines.add(sCurrentLine);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

有人这样想就好了。。我并不是说你不能用扫描器。。我觉得这样更好。。但这是一个选择的问题,不是大的架构问题。。考虑是你的:

< P>第一个要考虑的问题是:@ SoTiRiOS DelimaloLIS说,你可以从错误的TXT文件中读取。

第二个问题是,如果您完全确定是否要读取正确的.txt文件,那么解决方案是在附加到“lines”变量的同时使用reading scanner.hasNextLine()进行读取

我认为当您使用“hasNext()”进行读取时会出现问题,它会逐个令牌地读取令牌,然后使用“scanner.nextLine()”进入下一步,这将转到下一行

例如,您可以使用以下命令:

Scanner scanner = null;

ArrayList<String> lines = new ArrayList<String>();

try {
    scanner = new Scanner(getClass().getResourceAsStream("/level.txt"));
} catch (Exception ex) {
    ex.printStackTrace();
}

while (scanner.hasNextLine()) { /* difference is here */
    lines.add(scanner.nextLine());
}

Main.main.log(lines.size() + " size");
Scanner=null;
ArrayList行=新的ArrayList();
试一试{
scanner=newscanner(getClass().getResourceAsStream(“/level.txt”);
}捕获(例外情况除外){
例如printStackTrace();
}
而(scanner.hasNextLine()){/*的区别就在这里*/
line.add(scanner.nextLine());
}
Main.Main.log(lines.size()+“size”);
编辑:

您可以使用以下代码并根据需要对其进行修改。 我认为问题也发生在您读取文件时。要读取该文件,可以使用new file()构造函数,而不是您自己的选择。见下文:

Scanner scanner = null;
ArrayList<String> lines = new ArrayList<String>();

try {
    scanner = new Scanner(new File("level.txt")); /* difference is here */
} catch (Exception ex) {
    ex.printStackTrace();
}

while (scanner.hasNextLine()) { /* difference is here */
    lines.add(scanner.nextLine());
}

System.out.println(lines.size()); // gives output 2.
Scanner=null;
ArrayList行=新的ArrayList();
试一试{
scanner=newscanner(新文件(“level.txt”);/*区别在这里*/
}捕获(例外情况除外){
例如printStackTrace();
}
而(scanner.hasNextLine()){/*的区别就在这里*/
line.add(scanner.nextLine());
}
System.out.println(lines.size());//给出输出2。

Java 7将文件读取到列表的一行程序:

List<String> lines = Files.readAllLines(
    Paths.get(getClass().getResource("/level.txt").toURI()),
    StandardCharsets.UTF_8
);
List line=Files.readAllLines(
path.get(getClass().getResource(“/level.txt”).toURI()),
标准字符集.UTF_8
);

我不认为您的文件包含您所说的内容。请打印
并查看列表中的内容。确实如此,但如果您不确信,请将我的代码复制到IDE中并自行测试。按预期打印2。基本上扫描仪更适合用于用户输入。。。而且它的api不是最好的。。对于读取文件,使用BufferReader更合适。。例如,看看这个链接,更容易使用并不意味着其他解决方案是错误的。另外,您正在从文件系统读取,而OP正在从类路径读取。修复这两件事,你应该会没事的。如果你想解析输入或从cmd获取输入,可以使用Scanner。。BufferReader只是读取纯文本,这就是这里的情况。你不同意吗?我如何从文件位于我的src中的位置读取,而不是从我的计算机上的任何位置读取?是的,我如何从上面这个示例中的类路径读取?@Chris you刚刚将其设置为四行程序!!对不起:)。。。“一个表达者”?这可以工作,但返回一些不可靠的字符串。我怎样才能使它返回纯文件(当然在列表中)你能用不稳定的字符串来表示吗?使用此代码,文件的所有行都在
列表中
。输出与以前完全相同,您可以在自己的IDE中尝试吗?我尝试了并给出了正确的解决方案。请看我的答案的编辑版本。嗯,我的答案不起作用。但正在读取该文件,因为如果不存在该文件,则会出错。.rtf是否可以正常工作?发送文件名时不要使用/tag。这可能会引起问题。
Scanner scanner = null;

ArrayList<String> lines = new ArrayList<String>();

try {
    scanner = new Scanner(getClass().getResourceAsStream("/level.txt"));
} catch (Exception ex) {
    ex.printStackTrace();
}

while (scanner.hasNextLine()) { /* difference is here */
    lines.add(scanner.nextLine());
}

Main.main.log(lines.size() + " size");
Scanner scanner = null;
ArrayList<String> lines = new ArrayList<String>();

try {
    scanner = new Scanner(new File("level.txt")); /* difference is here */
} catch (Exception ex) {
    ex.printStackTrace();
}

while (scanner.hasNextLine()) { /* difference is here */
    lines.add(scanner.nextLine());
}

System.out.println(lines.size()); // gives output 2.
List<String> lines = Files.readAllLines(
    Paths.get(getClass().getResource("/level.txt").toURI()),
    StandardCharsets.UTF_8
);