Java Importing Question???

JC0133

Senior member
Nov 2, 2010
201
1
76
This is probably a dumb question but I am getting an compile error and I trying to see if this can cause it.

For this statement the "data.txt" . Are we suppose to just put the name in for data.txt or the path "C:\userName\desktop\data.txt" ??
File file = new File("data.txt");

my other question is how does the try - catch statement work?
 

Doublejr

Senior member
Jul 25, 2004
205
0
0
iirc
File file = new File("data.txt"); // will create/open data.txt in the directory of the executable

pseudocode
try { open files etc}
catch(exception e){
do stuff on exception}
 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
It depends. The program has a default path, and if the file is there, no problem. Else, you'll have to specify the path where the file.

a try block tries to execute some code, if throws an exception, the catch block will execute.

Code:
try{
createfile("PATH THAT DOESN'T EXIST");
}catch(IOException e)
{
output(e.what())
}
 

BrightCandle

Diamond Member
Mar 15, 2007
4,762
0
76
When it comes to paths in Java if its in your source code remember to escape those \'s.
For example

String filePath ="c:\\directory\\myfile.txt";
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
put data.txt under src/main/resources

then do

new File(this.getClass().getResource("/data.txt").toURI())