ファイルにStringを保存するメソッド作成

This commit is contained in:
r-ca 2024-01-08 11:56:24 +09:00
parent 15d454690e
commit a836410cbd
No known key found for this signature in database
GPG Key ID: 6A72911AC73464A9

View File

@ -175,14 +175,46 @@ public class FileManagerImpl implements FileManager {
} }
@Override @Override
public void saveText(String text, String fileName) { public void saveText(String text, String fileName) { // TODO-rca: リファクタリング // TODO-rca: 統合
createFile(fileName); if (isExist(fileName)) {
logger.debug("saveText", "file already exists");
// Overwrite
try {
Files.write(currentDir.resolve(fileName), text.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
else {
try {
Files.createFile(currentDir.resolve(fileName));
Files.write(currentDir.resolve(fileName), text.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
} }
@Override @Override
public void saveText(String text, Path path) { public void saveText(String text, Path path) {
if (isExist(path)) {
logger.debug("saveText", "file already exists");
// Overwrite
try {
Files.write(path, text.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
else {
try {
Files.createFile(path);
Files.write(path, text.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
} }
@Override @Override