java中使用Files.readLines()處理文本中行數(shù)據(jù)方式
使用Files.readLines()處理文本中行數(shù)據(jù)
開(kāi)發(fā)中遇到對(duì)數(shù)據(jù)庫(kù)導(dǎo)出到文件里的數(shù)據(jù)進(jìn)行處理,然后對(duì)處理后的數(shù)據(jù)再重新寫(xiě)回文件中,在這個(gè)過(guò)程中使用到了Files.readLines()方法
/**
*
* @param file : existed file
* @throws IOException
*/
public void lineProcess(File file) throws IOException {
Files.readLines(file, Charset.defaultCharset(), new LineProcessor() {
File outFile = new File("outfile");//處理后的數(shù)據(jù)輸出文件
List<String> lines = new ArrayList<String>();
@Override
public boolean processLine(String line) throws IOException {
String newLine = "";
//file中的 line數(shù)據(jù)格式:name,age,address -> NAME,AGE,ADDRESS,
String[] contents = line.split(",");
for (int i=0;i<contents.length;i++){
newLine.concat(contents[i].toLowerCase());
}
lines.add(newLine);
//將處理后的數(shù)寫(xiě)入新的文件 outFile
FileUtils.writeLines(outFile,lines,true);
lines.clear();
return true;
}
@Override
public Object getResult() {
try{
FileUtils.writeLines(outFile,lines,true);
}catch (Exception e){
e.getCause();
}
lines.clear();
return null;
}
});
}
方法中的LineProcessor()實(shí)現(xiàn)對(duì)每一行數(shù)據(jù)處理邏輯。
依賴guava
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
被readLine()折騰了一把
雖然寫(xiě)IO方面的程序不多,但BufferedReader/BufferedInputStream倒是用過(guò)好幾次的,原因是:
它有一個(gè)很特別的方法:readLine(),使用起來(lái)特別方便,每次讀回來(lái)的都是一行,省了很多手動(dòng)拼接buffer的瑣碎;
它比較高效,相對(duì)于一個(gè)字符/字節(jié)地讀取、轉(zhuǎn)換、返回來(lái)說(shuō),它有一個(gè)緩沖區(qū),讀滿緩沖區(qū)才返回;一般情況下,都建議使用它們把其它Reader/InputStream包起來(lái),使得讀取數(shù)據(jù)更高效。
對(duì)于文件來(lái)說(shuō),經(jīng)常遇到一行一行的,特別相符情景。
這次是在藍(lán)牙開(kāi)發(fā)時(shí),使用兩個(gè)藍(lán)牙互相傳數(shù)據(jù)(即一個(gè)發(fā)一個(gè)收),bluecove這個(gè)開(kāi)源組件已經(jīng)把數(shù)據(jù)讀取都封裝成InputStream了,也就相當(dāng)于平時(shí)的IO讀取了,很自然就使用起readLine()來(lái)了。
發(fā)數(shù)據(jù)
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(conn.openOutputStream()));
int i = 1;
String message = "message " + i;
while(isRunning) {
output.write(message+"/n");
i++;
}
讀數(shù)據(jù)
BufferedReader input = new BufferedReader(new InputStreamReader(m_conn.openInputStream()));
String message = "";
String line = null;
while((line = m_input.readLine()) != null) {
message += line;
}
System.out.println(message);
上面是代碼的節(jié)選,使用這段代碼會(huì)發(fā)現(xiàn)寫(xiě)數(shù)據(jù)時(shí)每次都成功,而讀數(shù)據(jù)側(cè)卻一直沒(méi)有數(shù)據(jù)輸出(除非把流關(guān)掉)。經(jīng)過(guò)折騰,原來(lái)這里面有幾個(gè)大問(wèn)題需要理解:
誤以為readLine()是讀取到?jīng)]有數(shù)據(jù)時(shí)就返回null(因?yàn)槠渌黵ead方法當(dāng)讀到?jīng)]有數(shù)據(jù)時(shí)返回-1),而實(shí)際上readLine()是一個(gè)阻塞函數(shù),當(dāng)沒(méi)有數(shù)據(jù)讀取時(shí),就一直會(huì)阻塞在那,而不是返回null;因?yàn)閞eadLine()阻塞后,System.out.println(message)這句根本就不會(huì)執(zhí)行到,所以在接收端就不會(huì)有東西輸出。要想執(zhí)行到System.out.println(message),一個(gè)辦法是發(fā)送完數(shù)據(jù)后就關(guān)掉流,這樣readLine()結(jié)束阻塞狀態(tài),而能夠得到正確的結(jié)果,但顯然不能傳一行就關(guān)一次數(shù)據(jù)流;另外一個(gè)辦法是把System.out.println(message)放到while循環(huán)體內(nèi)就可以。
readLine()只有在數(shù)據(jù)流發(fā)生異常或者另一端被close()掉時(shí),才會(huì)返回null值。
如果不指定buffer大小,則readLine()使用的buffer有8192個(gè)字符。在達(dá)到buffer大小之前,只有遇到"/r"、"/n"、"/r/n"才會(huì)返回。
readLine()的實(shí)質(zhì)(下面是從JDK源碼摘出來(lái)的)
String readLine(boolean ignoreLF) throws IOException {
StringBuffer s = null;
int startChar;
synchronized (lock) {
ensureOpen();
boolean omitLF = ignoreLF || skipLF;
bufferLoop:
for (;;) {
if (nextChar >= nChars)
fill(); //在此讀數(shù)據(jù)
if (nextChar >= nChars) { /* EOF */
if (s != null && s.length() > 0)
return s.toString();
else
return null;
}
......//其它
}
private void fill() throws IOException {
..../其它
int n;
do {
n = in.read(cb, dst, cb.length - dst); //實(shí)質(zhì)
} while (n == 0);
if (n > 0) {
nChars = dst + n;
nextChar = dst;
}
}
從上面看出,readLine()是調(diào)用了read(char[] cbuf, int off, int len) 來(lái)讀取數(shù)據(jù),后面再根據(jù)"/r"或"/n"來(lái)進(jìn)行數(shù)據(jù)處理。
在Java I/O書(shū)上也說(shuō)了:
public String readLine() throws IOException
This method returns a string that contains a line of text from a text file. /r, /n, and /r/n are assumed to be line breaks and are not included in the returned string. This method is often used when reading user input from System.in, since most platforms only send the user's input to the running program after the user has typed a full line (that is, hit the Return key).
readLine() has the same problem with line ends that DataInputStream's readLine() method has; that is, the potential to hang on a lone carriage return that ends the stream . This problem is especially acute on networked connections, where readLine() should never be used.
小結(jié),使用readLine()一定要注意
讀入的數(shù)據(jù)要注意有/r或/n或/r/n
沒(méi)有數(shù)據(jù)時(shí)會(huì)阻塞,在數(shù)據(jù)流異?;驍嚅_(kāi)時(shí)才會(huì)返回null
使用socket之類的數(shù)據(jù)流時(shí),要避免使用readLine(),以免為了等待一個(gè)換行/回車符而一直阻塞
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
零基礎(chǔ)搭建boot+MybatisPlus的詳細(xì)教程
這篇文章主要介紹了零基礎(chǔ)搭建boot+MybatisPlus,首先需要?jiǎng)?chuàng)建數(shù)據(jù)庫(kù)表和創(chuàng)建boot項(xiàng)目使用mybatisplus操作數(shù)據(jù)庫(kù),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
Java SpringBoot集成ChatGPT實(shí)現(xiàn)AI聊天
ChatGPT已經(jīng)組件放開(kāi)了,現(xiàn)在都可以基于它寫(xiě)插件了,也許可以用它結(jié)合文字語(yǔ)音開(kāi)發(fā)一個(gè)老人小孩需要的智能的說(shuō)話陪伴啥的,這篇文章就介紹SpringBoot結(jié)合ChatGPT實(shí)現(xiàn)AI聊天感興趣的同學(xué)可以借鑒一下2023-04-04
Java實(shí)現(xiàn)多項(xiàng)式除法的代碼示例
今天小編就為大家分享一篇關(guān)于Java實(shí)現(xiàn)多項(xiàng)式除法的代碼示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
Java對(duì)象在內(nèi)存中的布局是如何實(shí)現(xiàn)的?
Java對(duì)象在內(nèi)存中屬于oop-klass二分模型,即對(duì)象的實(shí)例數(shù)據(jù)和對(duì)象類型的元數(shù)據(jù)(字段定義、方法、常量池等元數(shù)據(jù))是分開(kāi)存儲(chǔ)的.而由于JVM對(duì)對(duì)象內(nèi)相同寬度的字段分配在一起,所以只要指定了字段類型分配的順序,就可以計(jì)算出每種類型字段相對(duì)于當(dāng)前對(duì)象的偏移起始位置2021-06-06
利用Java實(shí)現(xiàn)在PDF中添加工具提示
這篇文章主要介紹了如何通過(guò)Java在PDF中添加工具提示,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定的參考價(jià)值,感興趣的可以學(xué)習(xí)一下2022-01-01

