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!

10
  • 1
    Do all the directories WWW, KG1, 2 exist in your working directory? Commented Dec 24, 2019 at 12:43
  • Maybe due to this? stackoverflow.com/questions/8075373/… Commented Dec 24, 2019 at 12:50
  • @RealSkeptic: yes they do exist Commented Dec 24, 2019 at 17:19
  • Are you sure that they exist in uppercase? Commented Dec 24, 2019 at 17:25
  • @RealSkeptic: yes verified this as well, I even copy-pasted the path to make sure no typos Commented Dec 24, 2019 at 17:51

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I would appreciate to know the reason behind this?
I've always used File.separatorChar in situations like this (as opposed to File.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 '\\'.
@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: 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.
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

1 Comment

Thank you for your response, it worked as well: do you know the reason of this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.