我想閱讀整個文本文件並將其全部內容存儲到一個字符串中。然後我想將字符串打印到控制檯窗口。我嘗試這樣做:如何讀取文本文件並將其打印到控制檯窗口? Java
import java.util.Scanner;
import java.io.*;
public class WritingTextFiles{
public static void main (String [] args) throws IOException{
FileWriter fw= new FileWriter("testing.txt");
Scanner in= new Scanner (System.in);
String testwords=in.nextLine();
fw.write(testwords);
BufferedReader r = new BufferedReader(new FileReader("testing.txt"));
System.out.print(r);
fw.close();
}
}
被打印到控制檯窗口的唯一事情是[email protected]。
任何人都可以解釋這一點像我這樣的新手嗎?我的經驗很少,但我肯定願意學習。我願意接受任何和所有建議。提前致謝!
查看BufferedReader類API中的read()和readLine()方法:http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html – 2012-04-24 22:44:10
http://stackoverflow.com/questions/3886201/java-outputting-text-file-to-console – assylias 2012-04-24 22:45:13