summaryrefslogtreecommitdiff
path: root/src/main/java/no/eliashaugsbakk/kompilator/IO/FileReaderWriter.java
blob: f4f7bbeb6432e6f10b45e5098f08b631fcc76c57 (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("Kunne ikke lese filen: " + inputFilePath, e);
    }
  }

  public void writeFile(File file) throws FileReaderWriterException {
    try {
      Files.writeString(Path.of(file.fileName()), file.fileBody());
    } catch (IOException e) {
      throw new FileReaderWriterException("Kunne ikke skrive filen: " + file.fileName(), e);
    }
  }

  public void deleteFile(String filePath) throws FileReaderWriterException {
    try {
      Files.delete(Path.of(filePath));
    } catch (IOException e) {
      throw new FileReaderWriterException("Kunne ikke slette filen: " + filePath, e);
    }
  }
}