小編這次要給大家分享的是Java如何實現(xiàn)按行分割大文件,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),皋蘭企業(yè)網(wǎng)站建設(shè),皋蘭品牌網(wǎng)站建設(shè),網(wǎng)站定制,皋蘭網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,皋蘭網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
簡介
工作的時候,需要將一個大的文本文件按行分割成幾個小文件。本來懶得寫,想網(wǎng)上copy一下得了,但是 google 了一遍,找了幾個代碼寫的有點亂,嘗試了之后發(fā)現(xiàn)效率太慢了,一個 1000000 行 200M 的文件,按每個文件 2000 行分割,要6分多鐘才能跑完。沒辦法自己寫了個,試了幾次,基本都是 4 秒內(nèi)跑完,貼出來記錄下,下次用就直接 copy 出來用。
代碼
public static List<File> splitDataToSaveFile(int rows, File sourceFile, String targetDirectoryPath) {
long startTime = System.currentTimeMillis();
List<File> fileList = new ArrayList<>();
log.info("開始分割文件");
File targetFile = new File(targetDirectoryPath);
if (!sourceFile.exists() || rows <= 0 || sourceFile.isDirectory()) {
return null;
}
if (targetFile.exists()) {
if (!targetFile.isDirectory()) {
return null;
}
} else {
targetFile.mkdirs();
}
try (FileInputStream fileInputStream = new FileInputStream(sourceFile);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
StringBuilder stringBuilder = new StringBuilder();
String lineStr;
int lineNo = 1, fileNum = 1;
while ((lineStr = bufferedReader.readLine()) != null) {
stringBuilder.append(lineStr).append("\r\n");
if (lineNo % rows == 0) {
File file = new File(targetDirectoryPath + File.separator + fileNum + sourceFile.getName());
writeFile(stringBuilder.toString(), file);
//清空文本
stringBuilder.delete(0, stringBuilder.length());
fileNum++;
fileList.add(file);
}
lineNo++;
}
if ((lineNo - 1) % rows != 0) {
File file = new File(targetDirectoryPath + File.separator + fileNum + sourceFile.getName());
writeFile(stringBuilder.toString(), file);
fileList.add(file);
}
long endTime = System.currentTimeMillis();
log.info("分割文件結(jié)束,耗時:{}秒", (endTime - startTime) / 1000);
} catch (Exception e) {
log.error("分割文件異常", e);
}
return fileList;
}
private static void writeFile(String text, File file) {
try (
FileOutputStream fileOutputStream = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter, 1024)
) {
bufferedWriter.write(text);
} catch (IOException e) {
e.printStackTrace();
}
}看完這篇關(guān)于Java如何實現(xiàn)按行分割大文件的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。
網(wǎng)站名稱:Java如何實現(xiàn)按行分割大文件
當(dāng)前網(wǎng)址:http://www.yijiale78.com/article16/gcssdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、面包屑導(dǎo)航、品牌網(wǎng)站建設(shè)、網(wǎng)站收錄、域名注冊、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)