package org.example.util;
import lombok.extern.java.Log;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;
import java.util.logging.Level;
/**
* 文件操作
*/
@Log
public class FileUtil {
/**
* 创建文件
*/
public static File createFile(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
try {
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
if (!parentFile.mkdirs()) {
log.log(Level.WARNING, "文件夹创建失败");
}
}
if (!file.createNewFile()) {
log.log(Level.WARNING, "文件创建失败");
}
} catch (Exception e) {
log.log(Level.WARNING, e.getMessage(), e);
}
}
return file;
}
/**
* 保存文件(字符串内容保存)
* 路径 内容 是否追加(默认不追加) 写入字符集(默认utf-8)
* 注:追加插入需要手动添加换行符
*/
public static Boolean save(String filePath, String content, Boolean append, Charset charset) {
if (append == null) {
append = false;
}
if (charset == null) {
charset = StandardCharsets.UTF_8;
}
File file = createFile(filePath);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append), charset))) {
// 将内容写入文件
writer.write(content);
} catch (IOException e) {
System.err.println("写入文件时发生错误:" + e.getMessage());
return false;
}
return true;
}
/**
* 保存文件(集合内容保存)
* 路径 内容 是否追加(默认不追加) 写入字符集(默认utf-8)
* collection:list,set,queue
*/
public static <T> Boolean save(String filePath, Collection<T> content, Boolean append, Charset charset) {
StringBuilder stringBuilder = new StringBuilder();
for (T str : content) {
stringBuilder.append(str);
stringBuilder.append("\n");
}
return save(filePath, stringBuilder.toString(), append, charset);
}
/**
* 保存文件(字符串内容保存)
* 路径 内容 (默认覆盖,字符集utf-8)
*/
public static Boolean save(String filePath, String content) {
File file = createFile(filePath);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), StandardCharsets.UTF_8))) {
// 将内容写入文件
writer.write(content);
} catch (IOException e) {
System.err.println("写入文件时发生错误:" + e.getMessage());
return false;
}
return true;
}
/**
* 保存文件(集合内容保存)
* 路径 内容 (默认覆盖,字符集utf-8)
* collection:list,set,queue
*/
public static <T> Boolean save(String filePath, Collection<T> content) {
StringBuilder stringBuilder = new StringBuilder();
for (T str : content) {
stringBuilder.append(str);
stringBuilder.append("\n");
}
return save(filePath, stringBuilder.toString());
}
/**
* 拷贝文件夹
* source:源目录
* target:目标目录
*/
public static void copyFileDirectory(String source, String target) {
File sourceFile = new File(source);
File targetFile = new File(target);
copyDirectory(sourceFile, targetFile);
}
/**
* 复制目录
*/
private static void copyDirectory(File in, File out) {
//判断目标目录是否存在
if (!out.exists()) {
//目标目录不存在,创建此目录
if (!out.mkdir()) {
log.log(Level.WARNING, "目录创建失败");
return;
}
}
//获取目标目录绝对路径
String path = out.getPath();
//获取源目录下的所有目录、文件,并遍历
File[] inFiles = in.listFiles();
if (inFiles == null) {
log.log(Level.WARNING, "获取目录信息失败,目录地址:" + in.getAbsolutePath());
return;
}
for (File file : inFiles) {
//更改目标路径
out = new File(path, file.getName());
//判断file是否为目录,
//如果是目录则复制此目录,如果不是则复制此文件
if (file.isDirectory()) {
copyDirectory(file, out);
} else {
copyFile(file, out);
}
}
}
/**
* 复制文件
*/
private static void copyFile(File in, File out) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//创建输入输出流
fis = new FileInputStream(in);
fos = new FileOutputStream(out);
//复制文件————读取源文件数据,写入到目标文件
byte[] data = new byte[1024];
int len;
while ((len = fis.read(data)) != -1) {
fos.write(data, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
log.log(Level.WARNING, e.getMessage(), e);
} finally {
//关闭流
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
log.log(Level.WARNING, e.getMessage(), e);
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
log.log(Level.WARNING, e.getMessage(), e);
}
}
}
}
/**
* 读取文件内容
*/
public static String getStringContent(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
return null;
}
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
log.log(Level.WARNING, e.getMessage(), e);
}
return stringBuilder.toString();
}
/**
* 指定字符集读取文件内容
* 默认字符集utf-8
*/
public static String getStringContent(String filePath, Charset charset) {
File file = new File(filePath);
if (!file.exists()) {
return null;
}
if (charset == null) {
charset = StandardCharsets.UTF_8;
}
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(Files.newInputStream(file.toPath()), charset))) {
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
log.log(Level.WARNING, e.getMessage(), e);
}
return stringBuilder.toString();
}
/**
* 读取文件内容,并按行分
*/
public static List<String> getListContent(String filePath) {
List<String> stringList = new ArrayList<>();
File file = new File(filePath);
if (!file.exists()) {
return null;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
stringList.add(line);
}
} catch (IOException e) {
log.log(Level.WARNING, e.getMessage(), e);
}
return stringList;
}
/**
* 指定字符集读取文件内容,并按行分
* 默认字符集utf-8
*/
public static List<String> getListContent(String filePath, Charset charset) {
List<String> stringList = new ArrayList<>();
File file = new File(filePath);
if (!file.exists()) {
return null;
}
if (charset == null) {
charset = StandardCharsets.UTF_8;
}
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(Files.newInputStream(file.toPath()), charset))) {
String line;
while ((line = reader.readLine()) != null) {
stringList.add(line);
}
} catch (IOException e) {
log.log(Level.WARNING, e.getMessage(), e);
}
return stringList;
}
public static void main(String[] args) {
String path = "D:\\tmp\\test\\test\\test.txt";
Set<String> stringList = new HashSet<>();
stringList.add("1112212");
stringList.add("134234234");
Boolean save = save(path, stringList);
}
}
实习总结的java相关文件操作,如有问题请多担待。