File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes coding problem in save button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "coding problem in save button" Watch "coding problem in save button" New topic
Author

coding problem in save button

karan kalucha
Greenhorn

Joined: Apr 26, 2012
Posts: 4
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

Joined: Mar 30, 2012
Posts: 124

Problem is here -

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

Joined: Oct 13, 2005
Posts: 38793
    
  23
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

Joined: Apr 26, 2012
Posts: 4
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

Joined: Mar 30, 2012
Posts: 124

give path of some existing file there. you cant read from a non existing file.
karan kalucha
Greenhorn

Joined: Apr 26, 2012
Posts: 4
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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
 
subject: coding problem in save button