Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

writing to a file

 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me how i can write a string to a file?
I've already got

but clearly in is an InputStream and saveTable.getText(); is a string. How can i get around this?
 
Aleksey Matiychenko
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need InputStream in this case at all. You just write a string. Check the methods on FileOutputStream.
 
Chris Stehno
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better yet, if you are only writing character data, you should use Writers, specifically the BufferedWriter...
String text = "This is text to write to file.";
File = new File("yourfile.whatever");
FileWriter fw = new FileWriter(file);
BufferedWriter writer = new BufferedWriter(fw);
writer.write(text);
writer.close();
FileWriter will do this by itself, but the BufferedWriter povides some buffering for more efficiency.
Hope this helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic