Featured image of post 【合集】Spring-文件操作

【合集】Spring-文件操作

创建临时文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// 要放入临时文件的文件夹路径
String tempStr = AllTest.class.getResource("/").getPath() + "temp";

// 使用uuid,不覆盖
File tempFile;
try {
    tempFile = File.createTempFile(tempStr + UUID.randomUUID(), ".txt");
} catch (IOException e) {
    log.error("创建临时文件失败", e);
    throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "创建临时文件失败", e);
}

// 会覆盖
File tempFile;
try {
    tempFile = File.createTempFile(tempStr + "demo", ".txt");
} catch (IOException e) {
    log.error("创建临时文件失败", e);
    throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "创建临时文件失败", e);
}

获取resource目录下文件

img_143.png

方式1:直接getResource

1
2
String urlStr = 类名.class.getResource("/").getPath() + "easyExcel" + File.separator + "demo.xlsx";
File file = new File(urlStr);

方式2:classLoader方式

1
2
String urlStr = 类名.class.getClassLoader().getResource("").getPath() + "easyExcel" + File.separator + "demo.xlsx";
File file = new File(urlStr);

方式3:ResourceLoader方式

1
2
3
4
5
6
7
8
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource("classpath:" + File.separator + "easyExcel" + File.separator + "demo.txt");
try {
    File file = resource.getFile();
} catch (IOException e) {
    log.error("读取文件失败", e);
    throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "读取文件失败", e);
}

在resource目录下创建文件夹

1
2
3
4
String folderStr = AllTest.class.getResource("/").getPath() + "folder";
File folder = new File(folderStr);
boolean mkdir = folder.mkdir();
log.info("创建文件夹: {}", mkdir);

img_144.png

文件路径Str 转 File

1
File file = new File(urlStr);

复制文件

1
2
Files.copy(file1, file2);
FileCopyUtils.copy(file1, file2);

移动文件 或 重命名文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// 源文件
String sourceUrlStr = AllTest.class.getResource("/").getPath() + "easyExcel" + File.separator + "extra.xlsx";
File sourceFile = new File(sourceUrlStr);
// 目标文件(必须不存在,如果存在则报错,也就是说不可覆盖)
String targetUrlStr = AllTest.class.getResource("/").getPath() + "easyExcel" + File.separator;
File targetFile = new File(targetUrlStr + "demoNew2.txt");
// 移动文件
try {
    Files.move(sourceFile.toPath(), targetFile.toPath());
} catch (IOException e) {
    log.error("文件移动失败", e);
    throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "文件移动失败", e);
}

img_145.png

img_146.png

目录复制(递归复制)

1
FileSystemUtils.copyRecursively(file1, file2);

删除整个目录

1
FileSystemUtils.deleteRecursively(directoryFile);

读取文件的一些方法

File 转 byte[]

1
2
3
4
5
6
7
byte[] byteArray;
try {
    byteArray = FileUtils.readFileToByteArray(file);
} catch (IOException e) {
    log.error("文件转字节数组异常", e);
    throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "文件转字节数组异常", e);
}

InputStream 转 byte[]

1
byte[] bytes = FileCopyUtils.copyToByteArray(inputStream);

Reader 转 String

1
String string = FileCopyUtils.copyToString(reader);

File 转 List

1
2
3
4
5
6
7
List<String> list;
try {
    list = Files.readLines(file, StandardCharsets.UTF_8);
} catch (IOException e) {
    log.error("文件读取异常", e);
    throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "文件读取异常", e);
}

写出文件的一些方法

byte[] 转 File

1
FileCopyUtils.copy(bytes, file);

byte[] 转 OutputStream

1
FileCopyUtils.copy(bytes, new FileOutputStream(file));

从输入流到输出流

1
2
3
FileCopyUtils.copy(new FileInputStream(file1), new FileOutputStream(file));

FileCopyUtils.copy(new FileReader(file1), new FileWriter(file));

String 转 OutputStream

1
FileCopyUtils.copy(string, new FileWriter(file));

File 转 InputStream

1
2
3
4
5
6
try (InputStream inputStream = new FileInputStream(file)) {
    // 在这里处理输入流
} catch (IOException e) {
    log.error("文件转InputStream失败", e);
    throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "文件转InputStream失败", e);
}

MultipartFile 转 File

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
String urlStr = AllTest.class.getResource("/").getPath() + "easyExcel" + File.separator + "demo.xlsx";
File file = new File(urlStr);

// MultipartFile 转 File
try (InputStream inputStream = multipartFile.getInputStream();
     FileOutputStream outputStream = new FileOutputStream(file)) {
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
} catch (IOException e) {
    log.error("文件写入失败", e);
    throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "文件写入失败", e);
}
皖ICP备2024056275号-1
发表了80篇文章 · 总计150.57k字
本站已稳定运行