1 将数据内容写入一个dat/txt文件
引入依赖
<!-- io常用工具类 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
public void table() {
String path = "D:/yuepengcheng/table"; //目录名字
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
OutputStreamWriter writer = null;
//指定文件的编码
try {
// writer = new OutputStreamWriter(new FileOutputStream(file.getPath() + "/文件1.dat"),"GBK");
writer = new OutputStreamWriter(new FileOutputStream(file.getPath() + "/文件2.dat"), "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
StringBuilder builder = new StringBuilder();
//将指定的内容写进去
builder.append("dbId");
builder.append("|+|");
builder.append("dbName");
builder.append("$END$");
//for循环完毕 再关闭流
try {
writer.write(builder.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
log.info("完成信息"); //日志中是这样的表现形式 2024-02-19 15:24:24.905 INFO 7824 --- [ main] c.e.d.pms.service.FileWriteService : 完成信息
}
示例:
2 将指定路径下的多个文件打包成1个压缩包
/**
* 将多个文件打成一个压缩包 -- 多次执行会覆盖压缩包 不报错3
*/
public void tar() {
//创建被压缩文件对象
File file1 = new File("D:/yuepengcheng/table" + "/文件1.dat");
File file2 = new File("D:/yuepengcheng/table" + "/文件2.dat");
ArrayList<File> fileList = new ArrayList<>();
fileList.addAll(Arrays.asList(file1, file2));
//生成压缩包文件名字
String zipOutPath = "D:/yuepengcheng/table" + "/oracle.tar";
//根据文件名字得到文件所在目录
Path zipPath = Paths.get(zipOutPath);
// Path fileName = zipPath.getFileName(); // oracle.tar
//压缩包备份目录
String zipBackPath = "D:/yuepengcheng/table/zipBack";
//11 备份目录另一个写法
Path zipBackPath1 = Paths.get(zipBackPath);
//创建备份File目录
File backFile = new File(zipBackPath);
// 11
// File backFile = new File(zipBackPath.toString());
//判断备份文件是否存在
if (!backFile.exists()) {
backFile.mkdirs();
}
//压缩文件生成
File zipOutFile = new File(zipOutPath);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(zipOutFile);
} catch (FileNotFoundException e) {
e.printStackTrace();// 打印异常栈 异常名字 原因:在哪个行 哪个实现类中
// java.io.FileNotFoundException: D:\yuepengcheng\table\oracle.tar (另一个程序正在使用此文件,进程无法访问。)
// at java.base/java.io.FileOutputStream.open0(Native Method)
// at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
// at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
// at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:187)
// at com.example.dockerbootproject.pms.service.FileWriteService.tar(FileWriteService.java:102)
log.error(e.toString()); //异常的中文名字 具体路径不会打印出来 2024-02-19 15:59:21.900 ERROR 11184 --- [ main] c.e.d.pms.service.FileWriteService : java.io.FileNotFoundException: D:\yuepengcheng\table\oracle.tar (另一个程序正在使用此文件,进程无法访问。)
}
//引入依赖
TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(fileOutputStream);
//将多个文件放到压缩包中
try {
for (File file : fileList) {
// TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file); //压缩包里面有目录层级 到文件1.dat
TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file,file.getName()); //压缩包里面没有目录层级 只有文件
FileInputStream fileInputStream = null;
//文件放入压缩包
tarArchiveOutputStream.putArchiveEntry(tarArchiveEntry);
//文件中内容移出来放到tar包里面的文件中
fileInputStream = new FileInputStream(file);
IOUtils.copy(fileInputStream, tarArchiveOutputStream);
//关闭包
tarArchiveOutputStream.closeArchiveEntry();
}
//备份文件 将tar包 源 目标 复制选项 有就替换 zipPath.getFileName() = oralce.tar
Files.copy(zipPath, zipBackPath1.resolve(zipPath.getFileName()), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}