• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

saving a text file using JFileChooser

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am very new to GUI and I've been experimenting with JFileChooser lately. I can choose a text file using JFileChooser and display it in a JTextArea. The problem is I don't know how to the opposite. Probably by obtaining some text from a JTextArea, storing it as a String and then using JFileChooser to navigate through system files, type in a file name, and have the chooser "Create" a file with the name that I specify and then writing the String to it.
Here is what I've don so far. I'm a noob to all of this and you help will be greatly appreciated.

final JFileChooser fc = new JFileChooser() ;
int returnVal = fc.showSaveDialog(jDesktopPane1) ;
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File current_file = fc.getCurrentDirectory() ;
try{current_file.createNewFile() ;}
catch(Exception e) {System.out.println("Somethin' went wrong") ;}

this does not seem to work!!
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java Tutorial is always a good place to go for examples. This chapter is a good introduction on how to work with files.
The code that you have written:

actually attempts to create a new file with the same name as an existing directory, so we would not expect that to work. What you want to do is invoke getSelectedFile() on the file chooser dialog and create a FileOutputStream (see tutorial above) using that File.
 
reply
    Bookmark Topic Watch Topic
  • New Topic