-1

I need to save a text File which is already created in a particular path given by JFileChooser. What I do basically to save is:

public void actionPerformed(ActionEvent e) {
   JFileChooser chooser = new JFileChooser();
   int status = chooser.showSaveDialog(null);
   if (status == JFileChooser.APPROVE_OPTION) {
      System.out.print(chooser.getCurrentDirectory());
      // Don't know how to do it
   }

How to save the text file in a path given by JFileChooser?

Albert
  • 142
  • 1
  • 8

3 Answers3

2

You want to add the following after if statement:

File file = chooser.getSelectedFile();
FileWriter fw = new FileWriter(file);
fw.write(foo);

where foo is your content.

EDIT:

As you want to write a text file, I'd recommend the following:

PrintWriter out = new PrintWriter(file);
BufferedReader in = new BufferedReader(new FileReader(original));
while (true)
{
    String line = in.nextLine();
    if (line == null)
        break;
    out.println(line);
}
out.close();

where original is the file containing data you want to write.

Mateusz
  • 3,038
  • 4
  • 27
  • 41
-1

create a new File object with the path and name for the file

File file = new File(String pathname)
scgr
  • 57
  • 7
-1

Try this:

public void actionPerformed(ActionEvent e) {
    JFileChooser chooser = new JFileChooser();
    int status = chooser.showSaveDialog(null);
    if (status == JFileChooser.APPROVE_OPTION) {
        FileWriter out=new FileWriter(chooser.getSelectedFile());
        try {
            out.write("insert text file contents here");
        }
        finally {
            out.close();
        }
    }
    // ...

You'll need the filename you want to save under in addition to the directory provided by chooser.getCurrentDirectory(), but that should do what you need it to. Of course, you'll need to write the save method that actually writes to the stream, too, but that's up to you. :)

EDIT: There's a much method to use, chooser.getSelectedFile(), that should be used here, per another answer in the thread. Updated to use that method.

EDIT: Since OP specified the file being written is a text file, I've added code to write the contents of the file. Of course, you'll need to replace "insert text file contents here" with the actual file contents to write.

sigpwned
  • 6,957
  • 5
  • 28
  • 48
  • The question is how to code `save( out )`, please add it to your answer – Aubin May 18 '13 at 16:02
  • @Aubin, I can't since I don't know what's being written. Is it a text file? Is it an Excel file? Is it a proprietary file format? There isn't enough information to provide that answer. However, it will surely use some flavor of the [`OutputStream` write methods](http://docs.oracle.com/javase/6/docs/api/java/io/OutputStream.html#write%28byte[]%29). As mentioned elsewhere, a [`Writer`](http://docs.oracle.com/javase/6/docs/api/java/io/Writer.html) may be more suitable, but just replace `FileOutputStream` with [`FileWriter`](http://docs.oracle.com/javase/6/docs/api/java/io/FileWriter.html). – sigpwned May 18 '13 at 16:06
  • I need to save the text File which is already created not a new one :( – Albert May 18 '13 at 16:13
  • This code will save to the file chosen in the JFileChooser. This code will work whether or not the file chosen already exists. Specifically, if the file chosen already exists, this code will overwrite it; if the file chosen does not already exist, this code will create it. Regardless, the file will contain the provided string content once writing is complete. – sigpwned May 18 '13 at 16:16
  • But I need to save a created file not a new one – Albert May 18 '13 at 16:20
  • @Albert, I guess I don't understand what you're trying to do, which means the question you're asking is not clear. If you update your question with an [SSCCE](http://sscce.org/), either I or someone else may be able to help more. – sigpwned May 18 '13 at 16:22