如何从文本文件 Java 中读取单个单词(或行)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31370940/
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-11 10:59:05  来源:igfitidea点击:

How to read a single word (or line) from a text file Java?

javatextiotext-filesinputstream

提问by Ashwin Gupta

Like the title says, im trying to write a program that can read individual words from a text file and store them to Stringvariables. I know how to use a FileReaderor FileInputStreamto read a single charbut for what I'm trying to this wont work. Once I input the words I am trying to compare these with other String variables in my program using .equals so it would be best if I can import as Strings. I am also okay with inputting an entire line from a text file as a String in which case Ill just put one word on each line of my file. How do I input words from a text file and store them to String variables?

正如标题所说,我正在尝试编写一个程序,该程序可以从文本文件中读取单个单词并将它们存储到String变量中。我知道如何使用 aFileReaderFileInputStream阅读单曲,char但对于我正在尝试的内容,这是行不通的。输入单词后,我尝试使用 .equals 将这些单词与程序中的其他字符串变量进行比较,因此最好可以将其作为字符串导入。我也可以将文本文件中的一整行作为字符串输入,在这种情况下,我只在文件的每一行输入一个单词。如何从文本文件输入单词并将它们存储到字符串变量?

EDIT: Okay, that duplicate sort of helps. It might work for me but the reason my question is a little different is because the duplicate only tells how to read a single line. Im trying to read the individual words in the line. So basically splitting the line String.

编辑:好的,那种重复的帮助。它可能对我有用,但我的问题有点不同的原因是因为重复只告诉如何阅读一行。我正在尝试阅读该行中的单个单词。所以基本上拆分行字符串。

采纳答案by spork

To read lines from a text file, you can use this (uses try-with-resources):

要从文本文件中读取行,您可以使用它(使用 try-with-resources):

String line;

try (
    InputStream fis = new FileInputStream("the_file_name");
    InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
    BufferedReader br = new BufferedReader(isr);
) {
    while ((line = br.readLine()) != null) {
        // Do your thing with line
    }
}

More compact, less-readable version of the same thing:

同一事物的更紧凑、可读性更低的版本:

String line;

try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("the_file_name"), Charset.forName("UTF-8")))) {
    while ((line = br.readLine()) != null) {
        // Do your thing with line
    }
}

To chunk a line into individual words, you can use String.split:

要将一行分成单独的单词,您可以使用String.split

while ((line = br.readLine()) != null) {
    String[] words = line.split(" ");
    // Now you have a String array containing each word in the current line
}

回答by Michele Lacorte

You must use StringTokenizer! here an example and read this String Tokenizer

您必须使用 StringTokenizer!这里有一个例子并阅读这个String Tokenizer

private BufferedReader innerReader; 
public void loadFile(Reader reader)
        throws IOException {
    if(reader == null)
    {
        throw new IllegalArgumentException("Reader not valid!");
    }
        this.innerReader = new BufferedReader(reader);
    String line;
    try
    {
    while((line = innerReader.readLine()) != null)
    {
        if (line == null || line.trim().isEmpty())
            throw new IllegalArgumentException(
                    "line empty");
        //StringTokenizer use delimiter for split string
        StringTokenizer tokenizer = new StringTokenizer(line, ","); //delimiter is ","
        if (tokenizer.countTokens() < 4)
            throw new IllegalArgumentException(
                    "Token number not valid (<= 4)");
        //You can change the delimiter if necessary, string example
        /*
        Hello / bye , hi
        */
        //reads up "/"
        String hello = tokenizer.nextToken("/").trim();
        //reads up ","
        String bye = tokenizer.nextToken(",").trim();
        //reads up to end of line
        String hi = tokenizer.nextToken("\n\r").trim();
        //if you have to read but do not know if there will be a next token do this
        while(tokenizer.hasMoreTokens())
        {
          String mayBe = tokenizer.nextToken(".");
        }
    }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}

回答by Misha

These are all really complex answers. And I am sure they are all useful. But I prefer the elegantly simpleScanner:

这些都是非常复杂的答案。而且我相信它们都是有用的。但我更喜欢优雅简单的Scanner

public static void main(String[] args) throws Exception{
    Scanner sc = new Scanner(new File("fileName.txt"));
    while(sc.hasNext()){
        String s = sc.next();
        //.....
    }
}

回答by beresfordt

In java8 you can do something like the following:

在 java8 中,您可以执行以下操作:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Foo {
    public List<String> readFileIntoListOfWords() {
        try {
            return Files.readAllLines(Paths.get("somefile.txt"))
                .stream()
                .map(l -> l.split(" "))
                .flatMap(Arrays::stream)
                .collect(Collectors.toList());
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }
}

Though I suspect that the argument to split may need to be changed, eg to trim punctuation from the end of a word

尽管我怀疑 split 的参数可能需要更改,例如从单词末尾修剪标点符号