• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Saving files in .txt format using JFileChooser.

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I need to be able to save files in .txt format using JFileChooser. For this I have used the following filter:



This works as long as the user types the file name along with extn while saving(like ex.txt), but when only name is typed(like ex) and the file type is chosen as txt, the file is not saved. Can someone please tell me whr am going wrong?

Thanks in Advance,
Thejaswini.



 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the filename returned from file chooser, if it has ".txt" extension, do nothing, else append ".txt" to its name before saving it.
 
thejaswini ramesh
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rohit!

I have already done that and its still not working. Moreover, we need an instance of File as an argument for FileWriter - just appending .txt to the name is not helping.
[ May 03, 2007: Message edited by: thejaswini ramesh ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Moreover, we need an instance of File as an argument for FileWriter - just appending .txt to the name is not helping.


Does this mean you're not sure how to to create a File (or FileWriter) object if you have the file name as a string?
[ May 03, 2007: Message edited by: Ulf Dittmer ]
 
thejaswini ramesh
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does this mean you're not sure how to to create a File (or FileWriter) object if you have the file name as a string?



Well I have tried this as well:

File file = fc.getSelectedFile();
String fileName = file.getName();

String newFile = fileName + ".txt";

try{
boolean isFile = false;
if(!file.exists())
isFile = file.createNewFile(); // returns true which means file
// should have been created.

FileWriter outFile = new FileWriter(file);
PrintWriter out = new PrintWriter(outFile,true);

out.println("data");
out.close();
}
catch(IOException e) {}

But still the file is not saved as desired when user enters file name without explicitly specifying the extn.


 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would you even know what happens? Maybe there's an exception that is being ignored.

catch(IOException e) {}


This is almost always a bad idea, especially for IOException.
 
thejaswini ramesh
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How would you even know what happens? Maybe there's an exception that is being ignored.



Sorry, now that you mentioned it, I did use this within the catch block:

System.out.println("Exception in reating file:"+e.getMessage());

But no exception is being logged. Am not able to figure out what's wrong as my code doesn't work only whn name is typed(without extn).

Thanks Ulf. Any inputs would be greatly appreaciated.
 
rohit leeta
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Well, you create new file name (newFile) here but never use it! Try to use it..
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The next step is to call getAbsolutePath on the file object. You're only using the file name, not the directory - maybe the default directory isn't what you think it is?
 
thejaswini ramesh
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The next step is to call getAbsolutePath on the file object. You're only using the file name, not the directory - maybe the default directory isn't what you think it is?



Thanks a million Ulf

That was exactly what the problem was, infact the file was getting created but not in the path whr I was looking for it!
 
You got style baby! More than this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic