Java 讀取File to String
方法一-不指定檔案編碼格式: StringBuffer stringData = new StringBuffer(1000); BufferedReader reader = new BufferedReader(new FileReader(inputFileName)); char[] buf = new char[1024]; int numRead=0; while((numRead=reader.read(buf)) != -1){ String readData = String.valueOf(buf, 0, numRead); stringData.append(readData); buf = new char[1024]; } reader.close(); System.out.println(stringData.toString()); 方法二-指定檔案編碼格式 UTF-8: StringBuffer stringData = new StringBuffer(1000); BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(inputFileName), " UTF8 ")); char[] buf = new char[1024]; int numRead=0; while((numRead=reader.read(buf)) != -1){ String readData = String.valueOf(buf, 0, numRead); stringData.append(readData); buf = new char[1024]; } reader.close(); System.out.println(str