I am trying to create a new File in Eclipse using the following Java code :-
File myFile = new File("sampleFile.txt");
myFile.createNewFile();
System.out.println(myFile.getCanonicalPath());
I printed the Canonical Path since I was not getting the file anywhere in my Project. Not in the context root (Its a code inside my Dynamic Web Project). Not in the src directory.
And I get the following output :-
E:\Softwares and Executables\eclipse for Java EE\sampleFile.txt
This is created inside the directory in which Eclipse is present. However, my workspace is following :-
D:\Code N Code\Practising Java\Eclipse Workspace\Web Workspace
What is the problem?
The problem is you’re giving a relative filename – which means it’s relative to the working directory of your process. You can set the working directory in the “Arguments” tab of the run configuration in Eclipse.
The default for running Java applications is within your workspace directory, but it sounds like Dynamic Web Projects work differently – I haven’t used a DWP myself, but normally for things like web projects, the container you’re running in gives you appropriate paths to create files relative to.