简体   繁体   English

如何使用BufferedReader将txt文件中的行读取到数组中

[英]How do I use BufferedReader to read lines from a txt file into an array

I know how to read in lines with Scanner , but how do I use a BufferedReader ? 我知道如何阅读Scanner ,但我如何使用BufferedReader I want to be able to read lines into an array. 我希望能够将行读入数组。 I am able to use the hasNext() function with a Scanner but not a BufferedReader , that is the only thing I don't know how to do. 我能够将hasNext()函数与Scanner但不是BufferedReader ,这是我唯一不知道该怎么做的事情。 How do I check when the end of the file text has been reached? 如何检查何时到达文件结尾?

BufferedReader reader = new BufferedReader(new FileReader("weblog.txt"));

String[] fileRead = new String[2990];
int count = 0;

while (fileRead[count] != null) {
    fileRead[count] = reader.readLine();
    count++;
}

The documentation states that readLine() returns null if the end of the stream is reached. 文档指出 ,如果到达流的末尾, readLine()将返回null

The usual idiom is to update the variable that holds the current line in the while condition and check if it's not null: 通常的习惯用法是更新在while条件中保存当前行的变量,并检查它是否不为null:

String currentLine;
while((currentLine = reader.readLine()) != null) {
   //do something with line
}

As an aside, you might not know in advance the number of lines you will read, so I suggest you use a list instead of an array. 另外,您可能事先不知道要读取的行数,因此我建议您使用列表而不是数组。

If you plan to read all the file's content, you can use Files.readAllLines instead: 如果您打算阅读所有文件的内容,则可以使用Files.readAllLines

//or whatever the file is encoded with
List<String> list = Files.readAllLines(Paths.get("weblog.txt"), StandardCharsets.UTF_8);

readLine() returns null after reaching EOF . readLine() 到达EOF 返回null

Just 只是

do {
  fileRead[count] = reader.readLine();
  count++;
} while (fileRead[count-1]) != null);

Of course this piece of code is not the recommended way of reading the file, but shows how it might be done if you want to do it exactly the way you attempted to ( some predefined size array, counter etc. ) 当然这段代码不是推荐的读取文件的方法,但是如果你想要按照你想要的方式(一些预定义的大小数组,计数器等)完成它,它会显示如何完成它。

using readLine() , try-with-resources and Vector 使用readLine()try-with-resourcesVector

    try (BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\weblog.txt")))
    {
        String line;
        Vector<String> fileRead = new Vector<String>();

        while ((line = bufferedReader.readLine()) != null) {
            fileRead.add(line);
        }

    } catch (IOException exception) {
        exception.printStackTrace();
    }

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

相关问题 如何从.txt文件读取填充的数组? - How do I read a populated array from a .txt file? 如何从 .txt 文件中读取二维数组? - How do I read a 2D array as it is from a .txt file? 如何使用BufferedReader在.txt文件中向上移动行? - How to move up lines in a .txt file with BufferedReader? 如何从文件中读取行并将其存储到数组中 - How do I read lines from a file and store them into an array 如何从txt文件读取行并将每行添加到LinkedList类型? - How do I read lines from a txt file and add each line to a type LinkedList? 使用BufferedReader从文件读取一组行 - Read a set of lines from a file using BufferedReader 如何从txt文件中读取2行 - How Can I Read 2 lines From txt File 如何使用BufferedReader将行放入ArrayList中,然后执行它们? - How do I use BufferedReader to put lines in an ArrayList and then execute them? Java:如何将单独的文本行从.txt文件保存到字符串数组? - Java: How do I save seperate lines of text to a string array from a .txt file? 如何将由ENTER键(在.txt中)分隔的行(从.txt中读取)添加到字符串arrayList的单独元素中? - How do I add lines (read from a .txt) separated by AN ENTER KEY (in .txt) into separate elements of string arrayList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM