0

I am generating a file using the following syntax

File file = new File("input.txt");

The problem is that it is saying that it is writing to the file but I am not able to locate where the file is created, I searched my entire workspace. The expectation was that it would be created in the same folder as my code which is executing.

Any ideas?

Rest of the code :

        File file = new File("input.txt");

        // if file doesnt exists, then create it
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
  • why not try to specify a target location. just to make sure it generates the file :) – Ryan Dec 12 '14 at 01:41
  • I tried giving it like this abc/input.txt where abc is a folder in my workspace, that did not help either, tried ./input.txt etc as well –  Dec 12 '14 at 01:42
  • If you're on Windows, look for C:\input.txt. That's happened to me – etherous Dec 12 '14 at 01:43
  • No, I am on Mac, but the OS shouldnt matter I guess? –  Dec 12 '14 at 01:44

2 Answers2

0

You could do a sop on the absolute path and you would get the path:

File file = new File("input.txt");
System.out.println("" + file.getAbsolutePath());

if (!file.exists()) {
    try {
    file.createNewFile();
    } catch (IOException e) {
    e.printStackTrace();
    }
}
Khanna111
  • 3,627
  • 1
  • 23
  • 25
  • Thanks, that helped in finding out that it is `/Users/username/Desktop/eclipse/Eclipse-EE.app/Contents/MacOS/input.txt` any idea on how I can move this to my workspace instead? –  Dec 12 '14 at 01:48
  • Specify the path as in: File file = new File("/Users/gaurav_khanna1/Documents/input.txt"); – Khanna111 Dec 12 '14 at 01:50
  • Yes, I tried that,but I want to give it a relative path instead of an absolute path. –  Dec 12 '14 at 01:51
  • that is a separate question from the earlier question in this thread. I would recommend asking a new question so as to get your answer quickly (but first check to see if that has already been asked before on this site.). – Khanna111 Dec 12 '14 at 01:54
0

When you create file through relative paths, Java uses System.getProperty("user.dir"). So, in your case the full path to file will be System.out.println(System.getProperty("user.dir") + "/input.txt");.

Everv0id
  • 1,862
  • 3
  • 25
  • 47