package com.sinosig.demo.report.service.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
public class FileTxt {
public static void main(String[] args) throws Exception {
String lp = "D:\\eastFiles\\textHbFile\\";
String[] list = new File(lp).list();
File all = new File(lp + "all.txt");
if (!all.exists()) {
all.createNewFile();
}
File file;
for (int i = 1; i < 10; i++) {
file = new File(lp + i + ".txt");
try (
FileInputStream fileInputStream = new FileInputStream(file);
FileOutputStream fileOutputStream = new FileOutputStream(all, true)
) {
byte[] buffer = new byte[1024 * 1024];
int len;
while (-1 != (len = fileInputStream.read(buffer))) {
fileOutputStream.write(buffer, 0, len);
}
if (i!=list.length){
fileOutputStream.write("\n".getBytes(StandardCharsets.UTF_8));
}
}
}
/*获取文件行数*/
LineNumberReader reader = new LineNumberReader(new FileReader(all));
reader.skip(all.length());
System.out.println("文件行数:" + (reader.getLineNumber() + 1));
reader.close();
/*JDK8*/
long lines = Files.lines(Paths.get(all.getPath())).count() + 1L;
System.out.println(lines);
}
}
Java IO[文件合并、获取行数]
最新推荐文章于 2022-07-27 18:40:17 发布