Possible Duplicate:
How to create a Java String from the contents of a file
我有一个 .txt 文件,我想将其保存在字符串变量中。我用 File f = new File("test.txt");
导入了文件。现在我试图将它的内容放在 String
中多变的。我找不到如何执行此操作的明确解释。
最佳答案
使用 Scanner
:
Scanner file = new Scanner(new File("test.txt"));
String contents = file.nextLine();
file.close();
当然,如果您的文件有多行,您可以多次调用nextLine
。
关于java - 如何将字符串设置为等于.txt 文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13851752/