import java.io.*;
import java.util.Scanner;
public class FileRead {
public static void main(String[] args) {
//輸入檔案名稱
System.out.println("請輸入檔案名稱:(temp.txt)");
Scanner sca = new Scanner(System.in);
String filename = sca.next();
File file = new File(filename);
StringBuffer contents = new StringBuffer();//StringBuffer類 字串變數
BufferedReader reader = null;//BufferedReader字元流 BufferedInputStream位元組流
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
//repeat until all lines is read
while ((text = reader.readLine()) != null) {
contents.append(text);
contents.append(System.getProperty("line.separator"));//讀完一行增加一個分行符號
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
//show file contents here
System.out.println("檔案內容為:");
System.out.println(contents.toString());
}
}