2

I am using this method to generate some turtle files .ttl in a sub-directory of my project:

public static void write(int id, int depth){
        try {

            FileWriter fw = null;
            switch (getName()){
            case ("KG1"):
                fw = new FileWriter("WWW/KG1/" + depth + "/" + id + ".ttl");
            break;

            case ("KG2"):
                fw = new FileWriter("WWW/KG2/" + depth + "/" + id + ".ttl");
            }

        // Write something

        fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

But I am having this exception when I have put my project in Ubuntu (it is still working fine in Windows) in the java class FileWriter:

java.io.FileNotFoundException: /WWW/KG1/2/0.ttl (No such file or directory)

I am using Eclipse Neon for both OSs, but it seems that Ubuntu is not happy about it.

Here is what I have tried so far:

  1. Adding write permissons to ALL files and directories under the main project directory

  2. Using absolute path instead of relative path, by using System.getProperty("usr.dir"), and plotting all the path string I am giving to FileWriter, but it does not work.

Any advice?

Thanks!

Betty
  • 237
  • 6
  • 16

2 Answers2

2

I would try using File.separator and make sure the parent directory exists. Here is an example (may have syntax issues).

final String WWW = "WWW";
final String KG1 = "KG1";
final String KG2 = "KG2";
final String extension = ".ttl";

int id = 1;
int depth = 1;

String filePath = "." // current dir
  + File.separator 
  + WWW 
  + File.separator 
  + KG1 
  + File.separator 
  + depth 
  + File.separator 
  + id 
  + extension;

File file = new File(filePath);
// make sure parent dir exists (else created)
file.getParentFile().mkdirs(); 
FileWriter writer = new FileWriter(file);
The_Cute_Hedgehog
  • 1,280
  • 13
  • 22
  • Thanks! I would appreciate to know the reason behind this? – Betty Dec 24 '19 at 17:53
  • I've always used [`File.separatorChar`](https://docs.oracle.com/javase/10/docs/api/java/io/File.html#separatorChar) in situations like this (as opposed to [`File.pathSeparatorChar`](https://docs.oracle.com/javase/10/docs/api/java/io/File.html#pathSeparatorChar). File.separatorChar is: _The system-dependent default name-separator character. This field is initialized to contain the first character of the value of the system property file.separator. On UNIX systems the value of this field is '/'; on Microsoft Windows systems it is '\\'._ – Kaan Dec 25 '19 at 04:28
  • @Betty It's a concept named as absolute and relative paths. Absolute paths are complete but relative paths need more information to be complete. `www/kg1/..` is a relative path (no prefix). It's completed by JVM to be relative to `user.dir` property not current working directory. Please check out the output for `System.getProperty("user.dir")` not `usr.dir`. – The_Cute_Hedgehog Dec 25 '19 at 12:29
  • @The_Cute_Hedgehog: I am aware of absolute/relative paths. Indeed, `System.getProperty("user.dir")` outputs the actual absolute path, and `System.getProperty("usr.dir")` outputs `null`. I was using the latter one to get the absolute to current running JVM direcorty, which didn't help. – Betty Jan 03 '20 at 11:41
2

You can make things easier for yourself by using Path and File objects. Here is a version that optionally creates the wanted directory if it doesn't exist

Path path = Paths.get("WWW", "KG1", String.valueOf(depth));
try {
    Files.createDirectories(path);
    FileWriter fw = new FileWriter(new File(path.toFile(), id + ".ttl"));
    fw.close();
} catch (IOException e) {
    e.printStackTrace();
}

Note that I intentionally skipped the switch to simplify the answer

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52