Spring Bootで内蔵ファイルを修正する方法は何ですか
Spring Bootで内部ファイルを修正する方法:
- java.nio.file.Files
- シンプルに、美しいものを美しいと言うだけだ
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
String filePath = "path/to/file.txt";
String newContent = "New content";
try {
Path path = Path.of(filePath);
Files.write(path, newContent.getBytes(), StandardOpenOption.WRITE);
} catch (IOException e) {
// 处理文件操作异常
}
- java.io.FileWriter
import java.io.FileWriter;
import java.io.IOException;
String filePath = "path/to/file.txt";
String newContent = "New content";
try {
FileWriter writer = new FileWriter(filePath);
writer.write(newContent);
writer.close();
} catch (IOException e) {
// 处理文件操作异常
}
コード内のファイルパスは、実際のファイルへのパスに置き換える必要があります。newContentにファイルに書き込む新しいコンテンツを記載します。IOExceptionを処理する必要があります。