从文本文件中读取一个字符串以插入到输入中

时间:2011-04-06 14:29:49

标签: java file-io

假设我的文件名为“abc.txt”

此文件有三个字符串“你好吗”

我想把字符串读作“你好吗”

并将其存储到字符串word2中?

我该怎么办?

1 个答案:

答案 0 :(得分:4)

try
{
    File file = new File("abc.txt");
    BufferedReader br = new BufferedReader(new FileReader(file));
    String word2 = br.readLine();
    br.close();
    //test:
    System.out.println(word2);
} catch (IOException e)
{
    // Something went wrong, eg: file not found
    e.printStackTrace();
}