blob: b25b719c7349bb958cd7fe8bfc63aacb56378a1d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package no.eliashaugsbakk.kompilator.IO;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileReaderWriter {
public File readFile(String inputFilePath) throws FileReaderWriterException {
Path path = Path.of(inputFilePath);
String rawName = path.getFileName().toString();
int dotIndex = rawName.lastIndexOf('.');
String fileName = (dotIndex == -1) ? rawName : rawName.substring(0, dotIndex);
try {
String fileBody = Files.readString(path);
return new File(fileName, fileBody);
} catch (IOException e) {
throw new FileReaderWriterException("Failed to read file: " + inputFilePath, e);
}
}
public void writeFile(File file) throws FileReaderWriterException {
try {
Files.writeString(Path.of(file.fileName()), file.fileBody());
} catch (IOException e) {
throw new FileReaderWriterException("Failed to write file: " + file.fileName(), e);
}
}
public void deleteFile(String filePath) throws FileReaderWriterException {
try {
Files.delete(Path.of(filePath));
} catch (IOException e) {
throw new FileReaderWriterException("Could not delete file: " + filePath, e);
}
}
}
|