• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File writer issue

 
Maya Pillai
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

I have added output.flush(); befire closing....still not working properly
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to I/O and Streams.
 
Maya Pillai
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Data is not written to the file.
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maya Pillai
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic