views:

32

answers:

1

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.

+3  A: 

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" );
tangens