🚀 feat(新增): 文件输入输出读取和写入
This commit is contained in:
parent
9d2894b141
commit
f7ce82cfab
|
@ -1,33 +0,0 @@
|
||||||
package cn.bunny;
|
|
||||||
|
|
||||||
import lombok.SneakyThrows;
|
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipInputStream;
|
|
||||||
|
|
||||||
public class FileTest {
|
|
||||||
@SneakyThrows
|
|
||||||
public static void main(String[] args) {
|
|
||||||
String zipFilePath = "D:\\MyFolder\\文档和软件\\图片.zip";
|
|
||||||
try (FileInputStream fis = new FileInputStream(zipFilePath);
|
|
||||||
ZipInputStream zis = new ZipInputStream(fis)) {
|
|
||||||
|
|
||||||
ZipEntry entry = zis.getNextEntry();
|
|
||||||
while (entry != null) {
|
|
||||||
System.out.println("正在读取: " + entry.getName());
|
|
||||||
|
|
||||||
// 这里可以添加代码来读取entry的内容
|
|
||||||
// 例如,如果entry是一个文件,你可以使用BufferedReader来读取文本文件的内容
|
|
||||||
|
|
||||||
// 读取下一个条目
|
|
||||||
entry = zis.getNextEntry();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
// File file = new File("D:\\MyFolder\\文档和软件\\图片.zip");
|
|
||||||
// ZipFile zipFile = new ZipFile(file);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,108 @@
|
||||||
|
package cn.bunny.file;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
public class FileTest {
|
||||||
|
/**
|
||||||
|
* * 读取和写入纯文本内容
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void paintTextTest() {
|
||||||
|
// 源文件路径
|
||||||
|
String source = "D:\\dir\\source\\plain text.md";
|
||||||
|
// 目标文件路径
|
||||||
|
String target = "D:\\dir\\target\\plain text.md";
|
||||||
|
|
||||||
|
try (
|
||||||
|
// 创建读取和写入流
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(source));
|
||||||
|
BufferedWriter writer = new BufferedWriter(new FileWriter(target))
|
||||||
|
) {
|
||||||
|
// 读取源文件并写入目标文件
|
||||||
|
String line = reader.readLine();
|
||||||
|
while (line != null) {
|
||||||
|
writer.write(line);
|
||||||
|
writer.newLine();
|
||||||
|
line = reader.readLine();
|
||||||
|
}
|
||||||
|
} catch (IOException exception) {
|
||||||
|
System.out.println(exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 读取和写入流媒体内容
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void streamMediaTest() {
|
||||||
|
// 开始复制时间
|
||||||
|
LocalDateTime start = LocalDateTime.now();
|
||||||
|
|
||||||
|
// 源文件路径
|
||||||
|
String sourcePath = "D:\\dir\\source\\video.ts";
|
||||||
|
// 目标文件路径
|
||||||
|
String targetPath = "D:\\dir\\target\\video.ts";
|
||||||
|
|
||||||
|
try (
|
||||||
|
// 创建FileInputStream来读取源文件
|
||||||
|
FileInputStream reader = new FileInputStream(sourcePath);
|
||||||
|
// 创建BufferedWriter来写入目标文件
|
||||||
|
FileOutputStream writer = new FileOutputStream(targetPath)
|
||||||
|
) {
|
||||||
|
// 创建缓冲区
|
||||||
|
byte[] buffer = new byte[1024 * 7];
|
||||||
|
int length;
|
||||||
|
|
||||||
|
// 读取源文件并写入目标文件
|
||||||
|
while ((length = reader.read(buffer)) > 0) writer.write(buffer, 0, length);
|
||||||
|
|
||||||
|
} catch (IOException exception) {
|
||||||
|
System.out.println(exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算时间差
|
||||||
|
LocalDateTime end = LocalDateTime.now();
|
||||||
|
Duration duration = Duration.between(start, end);
|
||||||
|
System.out.println("复制耗时: " + duration.toMillis() + " 毫秒");// 复制耗时: 614 毫秒
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 使用NIO
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void nioStreamMediaTest() {
|
||||||
|
// 开始复制时间
|
||||||
|
LocalDateTime start = LocalDateTime.now();
|
||||||
|
|
||||||
|
// 源文件路径
|
||||||
|
String sourcePath = "D:\\dir\\source\\video.ts";
|
||||||
|
// 目标文件路径
|
||||||
|
String targetPath = "D:\\dir\\target\\video.ts";
|
||||||
|
|
||||||
|
try (
|
||||||
|
FileInputStream inputStream = new FileInputStream(sourcePath);
|
||||||
|
FileOutputStream outputStream = new FileOutputStream(targetPath);
|
||||||
|
FileChannel sourceChannel = inputStream.getChannel();
|
||||||
|
FileChannel targetChannel = outputStream.getChannel()
|
||||||
|
) {
|
||||||
|
long position = 0;
|
||||||
|
long count = sourceChannel.size();
|
||||||
|
|
||||||
|
while (position < count) {
|
||||||
|
position += sourceChannel.transferTo(position, count - position, targetChannel);
|
||||||
|
}
|
||||||
|
} catch (IOException exception) {
|
||||||
|
System.out.println(exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算时间差
|
||||||
|
LocalDateTime end = LocalDateTime.now();
|
||||||
|
Duration duration = Duration.between(start, end);
|
||||||
|
System.out.println("复制耗时: " + duration.toMillis() + " 毫秒");// 复制耗时: 325 毫秒
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package cn.bunny;
|
package cn.bunny.zip;
|
||||||
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
|
@ -1,4 +1,4 @@
|
||||||
package cn.bunny;
|
package cn.bunny.zip;
|
||||||
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
Loading…
Reference in New Issue