在java中,我需要读取一个文本文件,然后以空格分隔一个单词输出到控制台

In java, I need to read a text file, and then output it to the console one word at a time separated by whitespace

到目前为止,我有以下代码。编码对我来说是一个新的爱好,我低于初学者。我从 Whosebug 复制并粘贴了这个以读取文本文件

    BufferedReader r = new BufferedReader( new FileReader( "test.txt" ) );
    String s = "", line = null;
    while ((line = r.readLine()) != null) {
        s += line;
    }
     

然后这个来自我发现一次打印一个单词的网站。

       int i;          // Position in line, from 0 to line.length() - 1.
       char ch;        // One of the characters in line.
       boolean didCR;  // Set to true if the previous output was a carriage return.
       
       
       System.out.println();
       didCR = true;
       
       for ( i = 0;  i < s.length();  i++ ) {
          ch = s.charAt(i);
          if ( Character.isLetter(ch) ) {
             System.out.print(ch);
             didCR = false;
          }
          else {
             if ( didCR == false ) {
                System.out.println();
                didCR = true;
             }
          }
          
       }
       
       System.out.println();  // Make sure there's at least one carriage return at the end.

我真的很想输出文本文件,一次一个单词,空格包含逗号和句点等字符。请帮忙!

这个有效:

import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
        String s = "";
        String line = "";
        while((line = reader.readLine()) != null){
            s = s + line + "\n";
        }
        reader.close();
        s= s + " ";
        String word = "";
        char c = 0;
        for(int i= 0 ;i<s.length();i++){
            c = s.charAt(i);
            if(c == ' ' || c=='\n' || c=='\t' || c =='\r' || c=='\f' ){
                System.out.print(word.length()!=0?(word + "\n"):"");
                word = "";
            }else{
                word = word + c;
            }
        }
    }
}

test.txt的内容:

hello world, this is a code.

输出:

hello
world,
this
is
a
code.

读取文件已经基本正确,只需添加打印字样:

BufferedReader r = new BufferedReader( new FileReader( "test.txt" ) ); //create a buffer to read the file
String line;
while ((line = r.readLine()) != null) { //read each line one-by-one
    String[] words = line.split("\s+"); //split at whitespace, the argument is a regular expression
    for( String word : words ) {
       //skip any empty string, see explanation below
       if( word.isEmpty() ) {
         continue;
       }

       //option 1: print each word on a new line
       System.out.println(word);

       //option 2: print words of a line still on one line
       System.out.print(word + " ");
    }

    //option 2: switch to a new output line
    System.out.println();
}

注意:使用选项 1 或 2 以获得所需的输出。

关于word.isEmpty()的一句话:

即使我们使用 split("\s+") 拆分更长的空白序列,您仍然可以在结果数组中得到空字符串。原因是如果一行以空格开头,例如 A,你会得到数组 ["", "A"],你可能不想打印第一个空字符串 - 所以我们检查并跳过那些。

关于split("\s+")的一句话:

参数是一个正则表达式,"\s+"表示表达式\s\需要在Java字符串中进行转义,所以我们加了一个反斜杠)。

\s是一个字符class,意思是“任何空格”,包括空格、换行符、回车returns等。末尾的+是量词意思“一个或多个”,也用于拆分较长的空白序列。如果您不添加 A B(3 个空格)的输入将导致 ["A","","","B"].

如果只想按空格拆分,请使用 " +"

作为其他答案的替代方案,这里有一个使用 Java 8 Streams 的简洁明了的解决方案:

Pattern word = Pattern.compile("\s+");
try (Stream<String> lines = Files.lines(Paths.get("test.txt"), Charset.defaultCharset())) {
    lines.flatMap(line -> word.splitAsStream(line).filter(s -> ! s.isEmpty()))
         .forEachOrdered(System.out::println);
}

如果您不想使用 Java 8 Stream,请使用您的搜索结果。

正如我之前在 中所说:

If you want words-by-whitespace instead of words-by-nonletter, just replace Character.isLetter(ch) with ! Character.isWhitespace(ch).

这里是问题的代码,并进行了更改。

我还修复了两个地方的换行问题。看看你能不能认出来。

String s = "";
try (BufferedReader r = new BufferedReader(new FileReader("test.txt"))) {
    String line;
    while ((line = r.readLine()) != null) {
        s += line + '\n';
    }
}

boolean didCR = true;
for (int i = 0; i < s.length(); i++) {
    char ch = s.charAt(i);
    if (! Character.isWhitespace(ch)) {
        System.out.print(ch);
        didCR = false;
    } else {
        if (didCR == false) {
            System.out.println();
            didCR = true;
        }
    }
}
if (didCR == false) {
    System.out.println();
}