• 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

coding problem in save button

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please help me find problem in the code below as it does not save file instead it tells that the system cannot find the file specified(There is no problem in program)...


public void file_saveAction()
{
JFileChooser filechooser_save = new JFileChooser();
int option = filechooser_save.showSaveDialog(frame);
if(option == JFileChooser.APPROVE_OPTION)
{
try
{
BufferedReader in = new BufferedReader(new FileReader("."));
BufferedWriter out =new BufferedWriter(new FileWriter(filechooser_save.getSelectedFile().getPath()));
int c=0;
c=in.read();
while(c!=-1)
{
out.write(c);
}
out.close();
}


catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
 
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem is here -

as dot will always give you the current directory which is obviously not a file.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is one problem. I think using read() and write() is another. You should be using the methods to read and write a whole line (and probably write new lines, too).
 
karan kalucha
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anurag Verma wrote:Problem is here -

as dot will always give you the current directory which is obviously not a file.


But i have also tried...
BufferedReader in =new BufferedReader(new FileReader(filechooser_save.getSelectedFile().getPath()));
still it says that the system cannot find the file specified...
 
Anurag Verma
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
give path of some existing file there. you cant read from a non existing file.
 
karan kalucha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anurag Verma wrote:give path of some existing file there. you cant read from a non existing file.


sir i am really sorry for not being able to reply for such a long time... I was not well..
first of all i would like to thank you that you are taking so much pain in helping me out with my project..
Sir i got your point that in my coding i had given path of the file that does not really exist...Can you please tell me how to read from a current file(as in the file in which you are presently working) without giving the name of the file...(as in file=new file(name of file)...)
In java "." represents the current directory is there anything that represents the current file???
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no such thing as "the current file". If you want your Java program to read from a file, then you have to give it the path (not just the name part, the full path) to a file which actually exists.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic