Reputation: 133
I'm working with the File class for the first time and am a little confused.
I wrote this basic code to see if a file on my desktop could be detected:
public static void main(String[]args){
File test= new File("abc.pdf");
if(test.exists()==true){
System.out.println("got it!");
}
else{System.out.println("try again");}
}
I know I'm missing a big step since the program can't seem to detect it. Can anyone tell me what else I need to look up? Thanks.
Upvotes: 2
Views: 131
Reputation: 8278
You need to specify absolute path. e.g:
File file = new File("C:\\abcfolder\\abc.pdf");
In order to get the desktop absolute path:
String desktopPath = WindowsUtils.getCurrentUserDesktopPath();
Then:
File file = new File(desktopPath+"\\abc.pdf");
Upvotes: 6
Reputation: 58848
If you're running this in Eclipse, it will look for the file in the project folder.
If you're running this from a command line, it will look in the current folder.
If you're running this from a jar file, it will look in the folder with the jar file, at least on Windows.
If you're running this somewhere else, it depends on how you're running it. In most IDEs it will be the project folder.
Upvotes: 3