简体   繁体   English

Java BufferedReader转换为字符串数组

[英]Java BufferedReader to String Array

I was looking through a lot of diffrent subjects here on stackoverflow but couldn't find anything helpful so far :/ 我在这里检查了很多关于stackoverflow的不同主题,但到目前为止找不到任何有用的东西:/

So this is my problem. 所以这是我的问题。 I am writing a filecopier. 我正在写文件复印机。 The problem occurs already at reading the file. 在读取文件时已经出现问题。 My test docoument got 3 lines of random text. 我的测试文档有3行随机文本。 All those 3 lines should get written in a string array. 所有这三行都应写入字符串数组中。 The problem is that only the 2nd line of the textdocument gets written in the array and I can't figure out why. 问题是只有textdocument的第二行被写入数组,而我不知道为什么。 Already debugged it, but didn't get me any further. 已经调试过了,但是没有进一步了解我。

I know there are diffrent solutions for a filecopier with diffrent classes etc. But I would really like to get it running with the classes I used here. 我知道对于具有不同类的文件复印机有不同的解决方案。但是我真的很想让它与我在这里使用的类一起运行。

    String[] array = new String[5];
    String datei = "test.txt";
    public String[] readfile() throws FileNotFoundException {
    FileReader fr = new FileReader(datei);
    BufferedReader bf = new BufferedReader(fr);
    try {
        int i=0;
        //String  Zeile = bf.readLine();
        while(bf.readLine() != null){
            array[i] = bf.readLine();
        //  System.out.println(array[i]);  This line is for testing
            i++;
        }
        bf.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return array;

You're calling readLine() twice for each iteration of the loop, thereby discarding every other line. 对于循环的每次迭代,您都要调用readLine()两次,从而丢弃其他所有行。 You need to capture the value returned by every call to readLine() , because each readLine() call advances the reader's position in the file. 您需要捕获每次readLine()调用返回的值,因为每个readLine()调用都会提高读取器在文件中的位置。

Here's the idiomatic solution: 这是惯用的解决方案:

String line;
while((line = bf.readLine()) != null){
    array[i] = line;
    i++;
}

Here you read 2 lines: 在这里您读了两行:

   while(bf.readLine() != null){
        array[i] = bf.readLine();
    //  System.out.println(array[i]);  This line is for testing
        i++;
    }

You have to change your Code to: 您必须将代码更改为:

   String line = null;
   while((line =bf.readLine()) != null){
        array[i] = line;
    //  System.out.println(array[i]);  This line is for testing
        i++;
    }

The problem is here : 问题在这里:

while(bf.readLine() != null)

readLine() reads a line and returns the same at the same time it moves to the next line. readLine()读取一行并在移至下一行的同时返回同一行。

So instead of just checking if the returned value was null also store it. 因此,不仅要检查返回的值是否为null还要存储它。

String txt = null;
while((txt = bf.readLine()) != null)
    array[i++] = txt;

I think its because you are calling readLine() twice. 我认为是因为您两次调用readLine()。 First time in the loop, and then second time when you put it in the array. 第一次循环,然后第二次将其放入数组。 So, it reads a line at the beginning of the loop (line 1), then first line of code inside the loop (line 2 that you see) 因此,它在循环的开头(第1行)读取一行,然后在循环中读取第一行代码(您看到的第2行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM