Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

File writer issue

 
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()
 
Ranch Hand
Posts: 451
1
IntelliJ IDE Spring Java
  • 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: 451
1
IntelliJ IDE Spring Java
  • 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.
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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: 451
1
IntelliJ IDE Spring Java
  • 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.
 
Rancher
Posts: 43016
76
  • 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: 43016
76
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic