Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

coding problem in save button

 
karan kalucha
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • 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());
}
}
}
 
Anurag Verma
Ranch Hand
Posts: 166
Hibernate Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem is here -

as dot will always give you the current directory which is obviously not a file.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 1
  • Mark post as helpful
  • send pies
  • 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: 166
Hibernate Spring Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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???
 
Paul Clapham
Sheriff
Pie
Posts: 20188
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic