0

I started working with some files, something easy.

I read that the File class support relative paths, so I tried something like:

File file_bg = new File("data\\bg.png");
if(file_bg.exists())
  flag_bg = true;

Ok, now, making some debugging I noticed that every File variable have as parent path the same path where the IDE is, instead of the path where my project is.

Is it a common behavior or I have to set up something before starting the sketch? Is it equal for java IDEs like NetBeans?

One more question, there is a way to keep a String variable in memory that contains the path of my .pde file?

Thanks a lot for the interest, waiting a response!

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
ingroxd
  • 995
  • 1
  • 12
  • 28
  • 1
    duplicate of http://stackoverflow.com/questions/3844307/how-to-read-text-file-from-relative-path-in-a-project, http://stackoverflow.com/questions/8638582/java-relative-file-paths – Alex Wittig Jan 30 '14 at 16:53
  • 1
    In Windows anyway, the *current directory* is set at the process level. In other words, this has nothing to do with the IDE. The runtime asks the operating system what the current path is. Not sure if this is a helpful answer. – Mike Christensen Jan 30 '14 at 16:54

1 Answers1

2

You don't need to store a string with that directory, Processing can give it to you with sketchPath() and dataPath()... Not particularly odd, google's first result for "processing sketch path" returns this

thus:

File file_bg = new File(dataPath("bg.png"));
Petros Koutsolampros
  • 2,790
  • 1
  • 14
  • 20
  • `dataPath("")` paradoxically doesn't return the data folder of the sketch, but a folder [`null\data\ `] inside the IDE folder. – ingroxd Jan 31 '14 at 20:46
  • how about sketchPath()? What version of Processing are you using? – Petros Koutsolampros Feb 01 '14 at 00:04
  • also try this hacky slashy method: `args[0].split("=")[1]` – Petros Koutsolampros Feb 01 '14 at 00:15
  • I am using the last version, `2.1.1`. `sketchPath()` returns the same path of `dataPath()` without entering in `data\ `. By now I set the drive folder [is an external hd] with `getAbsolutePath().getChar(0) + ":\\";` and then I add the ret of the path statically. – ingroxd Feb 01 '14 at 09:08