Forums Login/signup

coding problem in save button

+Pie Number of slices to send: Send
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());
}
}
}
+Pie Number of slices to send: Send
Problem is here -

as dot will always give you the current directory which is obviously not a file.
+Pie Number of slices to send: Send
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).
1
+Pie Number of slices to send: Send
 

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...
1
+Pie Number of slices to send: Send
give path of some existing file there. you cant read from a non existing file.
+Pie Number of slices to send: Send
 

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???
+Pie Number of slices to send: Send
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.

reply
reply
This thread has been viewed 1242 times.
Similar Threads
Unable to Overwrite
Problems with serialization / deserialization.
Jfilechooser saving files with file extension
Save File As UTF-8
GUI: Cut/Copy/Paste/Etc.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Jun 29, 2025 00:32:46.