1、實現(xiàn)目標(biāo)
讀取文件,將文件中的數(shù)據(jù)一行行的取出。
2、代碼實現(xiàn)
1)、方式1:
通過BufferedReader的readLine()方法。
/** * 功能:java讀取txt文件的內(nèi)容 步驟:1:先獲得文件句柄 2:獲得文件句柄當(dāng)做是輸入一個字節(jié)碼流,需要對這個輸入流進行讀取 * 3:讀取到輸入流后,需要讀取生成字節(jié)流 4:一行一行的輸出。readline()。 備注:需要考慮的是異常情況 * * @param filePath * 文件路徑[到達文件:如: D:/aa.txt] * @return 將這個文件按照每一行切割成數(shù)組存放到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.); } } catch (Exception e) { System.out.println("讀取文件內(nèi)容出錯"); e.printStackTrace(); } return list; }
2)、方式2
通過文件byte數(shù)組暫存文件中內(nèi)容,將其轉(zhuǎn)換為String數(shù)據(jù),再按照 “回車換行” 進行分割。
/** * 讀取filePath的文件,將文件中的數(shù)據(jù)按照行讀取到String數(shù)組中 * @param filePath 文件的路徑 * @return 文件中一行一行的數(shù)據(jù) */ 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;// 返回文件內(nèi)容,默認(rèn)編碼 }
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直接緩存整個文件到內(nèi)存----------------"); 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]); } }
結(jié)果:
-------使用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直接緩存整個文件到內(nèi)存----------------[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是將文件的一部分或全部數(shù)據(jù)讀取出來用BufferReader緩存起來,需要再沖緩存中取數(shù)據(jù),這樣比要得時候去文件中讀取要快一些。
方式2是一次把文本的原始內(nèi)容直接讀取到內(nèi)存中再做處理(暫時不考慮內(nèi)存大小),這樣做效率也會提高。同時,可以處理當(dāng)你使用第1方式用readLine()方法時,文件又有線程在不斷的向文件中寫數(shù)據(jù)【只處理現(xiàn)在已經(jīng)在文件中的數(shù)據(jù)】。另外,用readline()之類的方法,可能需要反復(fù)訪問文件,而且每次readline()都會調(diào)用編碼轉(zhuǎn)換,降低了速度,所以,在已知編碼的情況下,按字節(jié)流方式先將文件都讀入內(nèi)存,再一次性編碼轉(zhuǎn)換是最快的方式。
有錯誤的希望大牛不吝賜教。 想了解一下,
1、通過ftp取一個文件到本地,我如何判斷對方的文件是否已經(jīng)寫完了。
2、當(dāng)我使用上面的BufferedReader的readLine()方法一行行讀取文件的時候,我還向文件中添加數(shù)據(jù),會不會出現(xiàn)文件讀取結(jié)束不了的情況。
源碼下載:
https://github.com/zcr1007391008/demo 的TestReadAllFileToMemory。
致謝:感謝您的閱讀!
新聞熱點
疑難解答