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 File writer issue 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 "File writer issue" Watch "File writer issue" New topic
Author

File writer issue

Maya Pillai
Ranch Hand

Joined: Jul 17, 2008
Posts: 60
Hi,

I am trying to save a vCard file using java.
I can save the file but its contents are gone. Can anyone help me to rectify the issue? Please find the code below.

Writer output = null;
String text = "JAVA";
File file = new File("write.vcf");
output = new BufferedWriter(new FileWriter(file));
output.write(text);
res.setContentType("text/x-vcard");
res.setHeader("Content-Disposition", "attachment; filename="+file);
output.close()
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hello,

You might want to call a flush() before you close it. But I'm not sure if the same would not be called before it is closed(), automatically. I think it makes sense for the JVM to call it so that data is not lost.

Thanks,
Raj.
Maya Pillai
Ranch Hand

Joined: Jul 17, 2008
Posts: 60
Thanks for the reply.

I have added output.flush(); befire closing....still not working properly
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi,

When you execute is the data written to the file? If it is then try calling flush() and close() before you set it to the response.


Thanks,
Raj.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Moving to I/O and Streams.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maya Pillai
Ranch Hand

Joined: Jul 17, 2008
Posts: 60
Data is not written to the file.
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi,

For me :-



certainly creates a file named write.vcf in the same folder as the class file with contant JAVA. Check if you are looking at a similarly named file in a different folder, maybe.

Cheers,
Raj.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
What, exactly, are you trying to do? The code looks somewhat like servlet code (implying that you're trying to stream a file to the client), but since you're using a File object, the operation is entirely local to the servlet container.

"attachment; filename="+file

You probably want to use "+file.getName()" instead of just "+file".

If this is indeed a web app, then note that web apps do not have a concept of a default directory - which means that you need to use absolute paths, not relative paths like you have now.


Ping & DNS - updated with new look and Ping home screen widget
Maya Pillai
Ranch Hand

Joined: Jul 17, 2008
Posts: 60
The above code is working to me. It create a file with content JAVA. But the contents are gone when i try to save it the disk using the file browser.

It is a web application, purpose is user can save the generated file to their disk using the file browser window.


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
But the contents are gone when i try to save it the disk using the file browser.

That's because you're not actually streaming the data to the browser. The file you create on disk has nothing to do with anything that gets sent to the client.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File writer issue
 
Similar Threads
I need a vCard parser.
Reg: Not able to read double bytes using input stream through socket
vCard Parser
Java file.mkdir() does not work even after chmod set to 777
vCard parser