Kawamoto Takeshi
Kawamoto Takeshi

Reputation: 606

Reading and writing file from a runnable jar with no path specified

If I am a running a runnable jar and have the code:

File txtfile = new File("sampleText.txt");

Would the file be created? If so where would sampleText.txt be created? In Eclipse, this would be created in the working directory. I have read that it is not possible to write within a jar, if so what would happen to this file? Will JAVA create this file elsewhere?

Also, if it was created, will I be able to read it?

Upvotes: 2

Views: 533

Answers (2)

Blasanka
Blasanka

Reputation: 22437

According to your code line for creating new file.

File txtfile = new File("sampleText.txt");

You haven't specify locstion, that means the place that this code is runnig, is the place sampleText.txt is created.

After you created jar file. jar means you compressing the all the code. The code for creating sampleText.txt is also there. So if you created your jar elsewhere, that the place your file created.

Note: unless you specified a location, like:

File file = new File("C:\\somefile\\sampleText.txt");

If you specify a location like this, your file should be that location no matter where your jar is.

Upvotes: 0

Rarreen
Rarreen

Reputation: 38

The working directory in this instance would be whichever directory the jar is in. So, yes the file would be created, and yes you'll be able to read it.

Upvotes: 1

Related Questions