This commit is contained in:
ipz233_vvv
2025-01-15 14:01:27 +02:00
parent 396a88366f
commit 8a44dc1fbb
26 changed files with 70 additions and 59 deletions

View File

@@ -0,0 +1,44 @@
package com.example.template.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
public class FileReadWrite {
/**
* Writes text to file, throws runtime exception if something goes wrong
* @param file
* @param text
*/
public static void write(File file, String text) {
try{
file.getParentFile().mkdirs();
FileOutputStream outputStream = new FileOutputStream(file);
byte[] strToBytes = text.getBytes();
outputStream.write(strToBytes);
outputStream.close();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
/**
* Tries to read file, creates empty file if it does not exist and returns empty string
* @param file
* @return
*/
public static String read(File file) {
try{
return new String(Files.readAllBytes(file.toPath()));
}
catch(IOException e)
{
write(file,"");
return read(file);
}
}
}