写段练手的代码:
//1、扫码文件夹(假设D:/file/),根据文件名获取近5分钟刚生成的所有文件
//2、循环文件列表,
//3、读取文件
//4、转编码从ASNI转成UTF-8
//5、生成新文件,名字里加一个'_UTF8'
以下是满足您需求的Java代码,使用了Spring Boot框架和Java标准库:
import org.springframework.util.FileCopyUtils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FileProcessor {
private static final String INPUT_DIR = "D:/file/";
private static final String OUTPUT_DIR = "D:/output/";
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss");
public static void main(String[] args) throws IOException {
File inputDir = new File(INPUT_DIR);
File[] newFiles = inputDir.listFiles();
if (newFiles == null) {
System.out.println("No files found in the input directory.");
return;
}
List<File> recentlyCreatedFiles = getRecentlyCreatedFiles(newFiles, 5);
if (recentlyCreatedFiles.isEmpty()) {
System.out.println("No files found in the last 5 minutes.");
return;
}
recentlyCreatedFiles.forEach(file -> {
try {
byte[] inputBytes = FileCopyUtils.copyToByteArray(file);
String inputString = new String(inputBytes, Charset.forName("ASNI"));
String outputString = new String(inputBytes, StandardCharsets.UTF_8);
byte[] outputBytes = outputString.getBytes(StandardCharsets.UTF_8);
Path inputPath = Paths.get(file.getAbsolutePath());
Path outputPath = Paths.get(OUTPUT_DIR + file.getName().replace(".txt", "_UTF8.txt"));
Files.write(outputPath, outputBytes);
System.out.println("Processed file: " + file.getName() + " -> " + outputPath.getFileName());
} catch (IOException e) {
System.err.println("Error processing file: " + file.getName() + ". Skipping...");
e.printStackTrace();
}
});
}
private static List<File> getRecentlyCreatedFiles(File[] files, int count) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime fiveMinutesAgo = now.minusMinutes(5);
return Arrays.stream(files)
.filter(file -> isRecentlyCreated(file, fiveMinutesAgo))
.collect(Collectors.toList());
}
private static boolean isRecentlyCreated(File file, LocalDateTime timeThreshold) {
long creationTime = file.lastModified();
LocalDateTime creationTimeLocal = LocalDateTime.ofInstant(Instant.ofEpochMilli(creationTime), ZoneId.systemDefault());
return creationTimeLocal.isAfter(timeThreshold);
}
}
请注意,这段代码假设输入文件夹中的文件都是文本文件,且文件扩展名为“.txt”。如果文件编码不是“ASNI”,需要相应地修改Charset
的值。此外,还需要确保输出文件夹存在,否则无法写入新文件。
【以下回答由 GPT 生成】
问题描述:
我想使用Spring Boot框架编写代码,实现以下功能:
代码解决方案:
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.time.*;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FileTranscodingApplication {
public static void main(String[] args) {
SpringApplication.run(FileTranscodingApplication.class, args);
String folderPath = "D:/file/";
String suffix = "_UTF8";
LocalDateTime fiveMinutesAgo = LocalDateTime.now().minusMinutes(5);
List<File> recentFiles = getRecentFiles(folderPath, fiveMinutesAgo);
for (File file : recentFiles) {
String content = readFileContent(file);
String newContent = transcodeASCIItoUTF8(content);
String newFileName = file.getName() + suffix;
createNewFile(folderPath, newFileName, newContent);
}
}
public static List<File> getRecentFiles(String folderPath, LocalDateTime fiveMinutesAgo) {
try {
return Files.list(Paths.get(folderPath))
.filter(file -> {
try {
LocalDateTime lastModified = LocalDateTime.ofInstant(
Files.getLastModifiedTime(file).toInstant(),
ZoneId.systemDefault()
);
return lastModified.isAfter(fiveMinutesAgo);
} catch (IOException e) {
e.printStackTrace();
}
return false;
})
.map(Path::toFile)
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static String readFileContent(File file) {
try {
return new String(Files.readAllBytes(file.toPath()), StandardCharsets.US_ASCII);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static String transcodeASCIItoUTF8(String content) {
return new String(content.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
}
public static void createNewFile(String folderPath, String fileName, String content) {
try {
Files.write(Paths.get(folderPath + fileName), content.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
}
这段代码使用了Spring Boot框架,并实现了你提出的功能要求。它包含了用于获取近5分钟内生成的文件、读取文件内容、转码文件内容以及生成新文件的方法。你可以将该代码在Spring Boot项目中运行,替换folderPath
为你想要操作的文件夹路径,并在main
方法中执行相应的操作。
【相关推荐】