Java 使用 bufferedreader 读取包含多行的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20813525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:32:46  来源:igfitidea点击:

Read text containing multiple line using bufferedreader

java

提问by user3141707

I would like to know how to read a text file containing multiple lines in java using BufferedStreamReader. Every line has two words separated by (;) and I want to use split()String operation to separate the 2 words. I also need to compare each word to a word in a master arraylist. I'm having problems to continue.

我想知道如何使用 BufferedStreamReader 在 java 中读取包含多行的文本文件。每行有两个由 (;) 分隔的单词,我想使用split()String 操作来分隔这两个单词。我还需要将每个单词与主数组列表中的单词进行比较。我有问题要继续。

Here's my code:

这是我的代码:

{

FileInputStreamReader f = new FileInputStreamReader(C://Desktop/test.txt);

InputStreamReader reader = new InputStreamReader(f);

BufferedReader Buff = new BufferedReader (reader);

String Line = buff.readLine();

String t[] = Line.split(;);

}   

回答by sanket

You need a while loop to read all the lines. Here is an example http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

您需要一个 while 循环来读取所有行。这是一个例子http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

回答by Rakesh KR

Try

尝试

while((line=buff.readLine())!=null){
   System.out.println(line);
}

回答by jagmohan

Replace

代替

String Line = Buff.readLine();

with

// buffer for storing file contents in memory
StringBuffer stringBuffer = new StringBuffer("");
// for reading one line
String line = null;
// keep reading till readLine returns null
while ((line = Buff.readLine()) != null) {
    // keep appending last line read to buffer
    stringBuffer.append(line);
}

Now, you have read the complete file into StringBuffer, you do whatever you want.

现在,您已将完整文件读入StringBuffer,您可以随心所欲。

Hope this helps.

希望这可以帮助。

回答by PopoFibo

You can use BufferedReader to loop through each of the line encountered within the specified file. In order to get your words split by a ";", you can use .splitand can store the resulting array in a list.

您可以使用 BufferedReader 循环遍历指定文件中遇到的每一行。为了让您的单词被“;”分割,您可以使用.split并将结果数组存储在列表中。

Finally, combine all the lists to a single list which would inturn hold all the words present in your file.

最后,将所有列表合并为一个列表,该列表将包含文件中存在的所有单词。

List<String> words = Arrays.asList(line.split(";"));
list.addAll(words);

Now you would want to compare the retrieved list against a Master list containing all your records.

现在,您希望将检索到的列表与包含所有记录的主列表进行比较。

// Compare the 2 lists, assuming your file list has less number of
// records
masterList.removeAll(list);

The above statement can be used in reverse too; in case the file holds the master list of words. Alternatively, you can store the 2 lists in temporary lists and compare in whatsoever way your require.

上面的语句也可以反过来使用;如果文件包含单词的主列表。或者,您可以将 2 个列表存储在临时列表中,并以您需要的任何方式进行比较。

Here is the complete code:

这是完整的代码:

public static void main(String[] args) {
        String line;

        // List of all the words read from the file
        List<String> list = new ArrayList<String>();
        // Your original mast list of words against which you want to compare
        List<String> masterList = new ArrayList<String>(Arrays.asList("cleaner",
                "code", "java", "read", "write", "market", "python", "snake",
                "stack", "overflow"));

        BufferedReader reader;

        try {
            reader = new BufferedReader(new FileReader("testing.txt"));

            while ((line = reader.readLine()) != null) {
                // Add all the words split by a ; to the list
                List<String> words = Arrays.asList(line.split(";"));
                list.addAll(words);
            }

            // Compare the 2 lists, assuming your file list has less number of
            // records
            masterList.removeAll(list);

            System.out.println(masterList);

            reader.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

File which I have created looks like:

我创建的文件看起来像:

cleaner;code
java;read
write;market
python;snake

The output of the above code:

上面代码的输出:

[stack, overflow]