2

In java,

File f;
f = new File("myfile.txt");
if (!f.exists()) 
{
    f.createNewFile();
}

when excute the above code,which path is assigned in default, while specific path is not given?

Manrico Corazzi
  • 11,299
  • 10
  • 48
  • 62
Venkat
  • 20,802
  • 26
  • 75
  • 84

3 Answers3

4

The current directory, which you can get by calling:

new File('.').getCanonicalPath();
Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114
  • getAbsolutePath() may give a friendlier result on unix systems as getCanonicalPath() resolves symlinks... this may give odd results such as converting /home/me to an odd path such as: /mnt/server/dsk/homedirs/m/mydir – RedPandaCurios Feb 25 '10 at 10:06
  • Fair enough. Though for most/all purposes, it shouldn't matter if you're using the canonical or absolute path. – Chinmay Kanchi Feb 25 '10 at 14:55
2

The current directory.

RedPandaCurios
  • 2,264
  • 12
  • 20
2

The current directory. Apparently System.getProperty("user.dir") can get this for you.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358