// 定義文件物理路徑常量
final String utf8File=new String(application.getRealPath(""));
// 獲取某個文件的字節流
FileInputStream fis=new FileInputStream(utf8File);
// 按照 UTF-8 編碼方式將字節流轉化為字符流
InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
// 從字符流中獲取文本并進行緩沖
BufferedReader br=new BufferedReader(isr);
// 聲明并建立 StringBuffer 變量,用于存儲全部文本文件內容
StringBuffer sbContent=new StringBuffer();
// 聲明 String 變量,用于臨時存儲文本行內容
String sLine;
// 循環讀取文本文件每行內容
while((sLine=br.readLine())!=null){
// 去掉回車和換行符,去掉文本行前后空格,連接全部文本文件內容
sbContent=sbContent.append(sLine.replace("\n","").replace("\r","").trim());
}
// 輸出文本文件內容
out.print(new String(sbContent));