1. 文本文件的读取
文本的读取,返回值是一个list, 如果需要返回一整个string 在while循环中使用StringBuilder.append 即可
/**
* 逐行读取文本
*
* @param filePath 文件路径
* @return List<String>
*/
public static List<String> readTxtFile1(String filePath) throws IOException {
Path path = Paths.get(filePath);
//判断文件是否存在
if (!Files.exists(path)) {
log.error("file is not exist");
return null;
}
List<String> txtList = new ArrayList<>();
try (InputStreamReader read = new InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(read)) {
String lineTxt;
while (null != (lineTxt = bufferedReader.readLine())) {
if (StringUtils.isNotEmpty(lineTxt)) {
txtList.add(lineTxt);
}
}
}
return txtList;
}
2.文本文件的写入
/**
* 以指定的编码 写入数据
*/
private static void outputStreamWriter(String filePath, List<String> content, Charset charset, boolean append) throws IOException {
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(filePath, append), charset);
BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
for (String item : content) {
bufferedWriter.write(item);
bufferedWriter.newLine();
}
}
}
3.文件的拷贝
/**
* 简单的文件拷贝,不使用缓冲区,适用于小文件
*
* @param sourceFile 源文件
* @param targetFile 目标文件
*/
public static void copyFile(String sourceFile, String targetFile) throws IOException {
try (FileInputStream fileInputStream = new FileInputStream(sourceFile);
FileOutputStream fileOutputStream = new FileOutputStream(targetFile)) {
byte[] b = new byte[1024];
int length;
while ((length = fileInputStream.read(b)) != -1) {
fileOutputStream.write(b, 0, length);
// 不能用 fileOutputStream.write(b) 因为最后有可能读不够而出错
}
}
}
4.大文件的拷贝
/**
* 进行文件的拷贝-高效
* 使用字节处理流 字节缓冲输入流和字节缓冲输出流
*
* @param source 源
* @param target 复制到
* @return boolean 结果
*/
public static boolean BufferedStreamFileCopy(String source, String target) {
if (StringUtils.isEmpty(source) || StringUtils.isEmpty(target)) {
log.error("文件路径不存在! path:{}", source);
return false;
}
if (source.equals(target)) {
log.error("复制的源文件和目标文件不能是同一个文件! path:{}", source);
return false;
}
Path path = Paths.get(source);
boolean exists = Files.exists(path);
if (!exists) {
log.error("文件不存在! path:{}", source);
return false;
}
long currentTimeMillis = System.currentTimeMillis();
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(Files.newInputStream(path));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(Files.newOutputStream(Paths.get(target)))) {
byte[] bytes = new byte[1024];
int length;
while ((length = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, length);
}
log.info("copy file success, time:{} ms", System.currentTimeMillis() - currentTimeMillis);
return true;
} catch (IOException e) {
log.error("BufferedStreamFileCopy 拷贝文件异常", e);
return false;
}
}
5.文本文件编码转换
/**
* 编码转换- 如一个文件的编码是 gb2312 转为 utf-8
* 请注意,请用文件本身的正确的编码尝试读取,否则会乱码
*
* @param filePath 原始文件
* @param oldCharset 原始字符编码
* @param newFilePath 新文件
* @param newCharset 新字符编码
* @throws IOException io异常
*/
private static void conversionCharset(String filePath, String oldCharset, String newFilePath, String newCharset) throws IOException {
try (InputStreamReader reader = new InputStreamReader(Files.newInputStream(Paths.get(filePath)), oldCharset);
BufferedReader bufferedReader = new BufferedReader(reader);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(Files.newOutputStream(Paths.get(newFilePath)), newCharset);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter)) {
String line;
while (null != (line = bufferedReader.readLine())) {
bufferedWriter.write(line);
bufferedWriter.newLine();
}
}
}
玄机博客
© 版权声明
1.本站内容仅供参考,不作为任何法律依据。用户在使用本站内容时,应自行判断其真实性、准确性和完整性,并承担相应风险。
2.本站部分内容来源于互联网,仅用于交流学习研究知识,若侵犯了您的合法权益,请及时邮件或站内私信与本站联系,我们将尽快予以处理。
3.本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
4.根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
5.本站是非经营性个人站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途
THE END
暂无评论内容