views:

58

answers:

2

hi ,

i have a little confusion.. i want to write a string in a text file which should be dynamically saved by the saveDialog i have done this task statically.. means file with specified file name is created and text is also written from jTextArea.. but i wnt to save this file on my given location and with my given name.. can u please guide me in this regard?? below is code:

do{
    String fileData=jTextArea1.getText();
    byte buf[]=fileData.getBytes();

    JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("Text/fasta files", ".txt", ".fasta");
            chooser.setFileFilter(filter);
            int returnVal = chooser.showSaveDialog(null);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    System.out.println("inside try after retVal");
                    try{
                        //OutputStream f2=new FileOutputStream("filename.txt");
                        OutputStream f2=new FileOutputStream("file.txt");

                        f2.write(buf);
                        f2.close();
                        } catch (IOException ex) {
                            Logger.getLogger(CreatingFile.class.getName()).log(Level.SEVERE, null, ex);
                                                }


                } else {
                    return null;
                } //else ends


    // TODO add your handling code here:
}while(true);
A: 

Use

new FileOutputStream(chooser.getName());

instead of

new FileOutputStream("file.txt");
Zed
A: 

Use chooser.getSelectedFile()

Use a FileWriter, wrapped in a BufferedWriter, not a FileOutputStream.

And use the write(...) method of JTextArea to write out the text.

camickr