1、實現目標
讀取文件,將文件中的數據一行行的取出。
2、代碼實現
1)、方式1:
通過BufferedReader的readLine()方法。
/** * 功能:Java讀取txt文件的內容 步驟:1:先獲得文件句柄 2:獲得文件句柄當做是輸入一個字節碼流,需要對這個輸入流進行讀取 * 3:讀取到輸入流后,需要讀取生成字節流 4:一行一行的輸出。readline()。 備注:需要考慮的是異常情況 * * @param filePath * 文件路徑[到達文件:如: D:\aa.txt] * @return 將這個文件按照每一行切割成數組存放到list中。 */ public static List<String> readTxtFileIntoStringArrList(String filePath) { List<String> list = new ArrayList<String>(); try { String encoding = "GBK"; File file = new File(filePath); if (file.isFile() && file.exists()) { // 判斷文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding);// 考慮到編碼格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { list.add(lineTxt); } bufferedReader.close(); read.close(); } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("讀取文件內容出錯"); e.printStackTrace(); } return list; }
2)、方式2
通過文件byte數組暫存文件中內容,將其轉換為String數據,再按照 “回車換行” 進行分割。
/** * 讀取filePath的文件,將文件中的數據按照行讀取到String數組中 * @param filePath 文件的路徑 * @return 文件中一行一行的數據 */ public static String[] readToString(String filePath) { File file = new File(filePath); Long filelength = file.length(); // 獲取文件長度 byte[] filecontent = new byte[filelength.intValue()]; try { FileInputStream in = new FileInputStream(file); in.read(filecontent); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String[] fileContentArr = new String(filecontent).split("\r\n"); return fileContentArr;// 返回文件內容,默認編碼 }
3)、測試
public static void main(String[] args) { List<String> stringList = readTxtFileIntoStringArrList("C:\\soft\\java\\tomcat\\apache-tomcat-7.0.40\\webapps\\appDataGenerate\\log4j\\lepai_recognize_cache.log"); System.out.println("-------使用BufferedReader讀取-----------"); for(String str : stringList) { System.out.println(str); } System.out.println("\n---------使用byte直接緩存整個文件到內存----------------"); String[] stringArr = readToString("C:\\soft\\java\\tomcat\\apache-tomcat-7.0.40\\webapps\\appDataGenerate\\log4j\\lepai_recognize_cache.log"); for(int i = 0 ; i < stringArr.length ; i ++) { System.out.println(stringArr[i]); } }
結果:
-------使用BufferedReader讀取----------- [2015-11-30 13:21:28] [RecognizeCache] [INFO] : RecogizeCache init [2015-11-30 13:21:28] [RecognizeCache] [INFO] : RecogizeCache init [2015-11-30 13:21:28] [RecognizeCache] [INFO] : RecogizeCache init [2015-11-30 13:21:28] [RecognizeCache] [INFO] : RecogizeCache init [2015-12-01 14:52:04] [RecognizeCache] [INFO] : 讀取文件:4209bad42de0f6e55c0daf0bd24b635a.txt ---------使用byte直接緩存整個文件到內存---------------- [2015-11-30 13:21:28] [RecognizeCache] [INFO] : RecogizeCache init [2015-11-30 13:21:28] [RecognizeCache] [INFO] : RecogizeCache init [2015-11-30 13:21:28] [RecognizeCache] [INFO] : RecogizeCache init [2015-11-30 13:21:28] [RecognizeCache] [INFO] : RecogizeCache init [2015-12-01 14:52:04] [RecognizeCache] [INFO] : 讀取文件:4209bad42de0f6e55c0daf0bd24b635a.txt
3、比較
方式1是將文件的一部分或全部數據讀取出來用BufferReader緩存起來,需要再沖緩存中取數據,這樣比要得時候去文件中讀取要快一些。
方式2是一次把文本的原始內容直接讀取到內存中再做處理(暫時不考慮內存大小),這樣做效率也會提高。同時,可以處理當你使用第1方式用readLine()方法時,文件又有線程在不斷的向文件中寫數據【只處理現在已經在文件中的數據】。另外,用readline()之類的方法,可能需要反復訪問文件,而且每次readline()都會調用編碼轉換,降低了速度,所以,在已知編碼的情況下,按字節流方式先將文件都讀入內存,再一次性編碼轉換是最快的方式。
有錯誤的希望大牛不吝賜教。 想了解一下,
1、通過ftp取一個文件到本地,我如何判斷對方的文件是否已經寫完了。
2、當我使用上面的BufferedReader的readLine()方法一行行讀取文件的時候,我還向文件中添加數據,會不會出現文件讀取結束不了的情況。
源碼下載:
https://github.com/zcr1007391008/demo 的TestReadAllFileToMemory。
致謝:感謝您的閱讀!