user69514
user69514

Reputation: 27629

Java File manipulation

So I have an application with a JFileChooser from which I select a file to read. Then I change some words and write a new file. The problem that I am having is that when I write the new file it's saved in the project directory. How do I save it in the same directory as the file that I chose using the JFileChooser. Note: I don't want to use the JFileChooser to choose the location. I just need to save the file in the same directory as the original file that I read.

Upvotes: 1

Views: 455

Answers (1)

tangens
tangens

Reputation: 39733

You choose a file like this:

File fileToRead = JFileChooser.getSelectedFile();

Then you read and change the content and write it back to the same location with a different name:

File fileToWrite = new File( fileToRead.getParent(), "newName.txt" );

Upvotes: 3

Related Questions