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

时间:2015-07-12 18:06:56

标签: java text io text-files inputstream

就像标题所说,我试图编写一个程序,可以从文本文件中读取单个单词并将它们存储到String个变量中。我知道如何使用FileReaderFileInputStream来阅读单个char,但是我正在努力解决这个问题。一旦我输入单词,我试图将这些与我的程序中的其他String变量进行比较,使用.equals,这样最好是我可以导入为Strings。我也可以将文本文件中的整行输入为String,在这种情况下,我只在文件的每一行放一个单词。如何从文本文件中输入单词并将其存储到String变量中?

编辑: 好的,那种重复的帮助。它可能适合我,但我的问题有点不同的原因是因为副本只告诉如何读取单行。我试图阅读该行中的单个单词。所以基本上拆分线String。

4 个答案:

答案 0 :(得分:9)

要从文本文件中读取行,您可以使用它(使用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
    }
}

同一件事的更紧凑,不易阅读的版本:

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
    }
}

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

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

答案 1 :(得分:9)

这些都是非常复杂的答案。我相信它们都很有用。但我更喜欢优雅简单 result =L[:]

Scanner

答案 2 :(得分:1)

你必须使用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);
    }
}

答案 3 :(得分:1)

在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();
    }
}

虽然我怀疑可能需要更改分裂的参数,例如从单词的末尾修剪标点符号