How to set String equal to text in .txt file
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
How to create a Java String from the contents of a file
我有一个.txt文件要保存在字符串变量中。我用
使用
1 2 3 4 5 |
当然,如果您的文件有多行,您可以多次调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | BufferedReader br = new BufferedReader(new FileReader("file.txt")); try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(" "); line = br.readLine(); } String everything = sb.toString(); } finally { br.close(); } |