This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need help writing to a file  RSS feed

 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to write to an already existing file, but my problem is that once I write to the file everything
is overwritten. So prior to writing Legacy.txt contained john, joe, james and in the end it only
contains text, and i want it so that it would contain john,joe,james,text
can anyone help
Thank you for your time
if(OptionsPane.itemStr[i-1] == OptionsPane.itemStr[0])
{
FileOutputStream out;
PrintStream p;
try
{
out = new FileOutputStream("a:\\Legacy.txt");
p = new PrintStream(out);
p.println("text");
p.close();
}
catch(Exception exception)
{
}
}

 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
You almost have it. Use the overloaded version of the c'tor to pass in a flag to tell the FileOutputStream that you want to append to the file rather than overwrite.

public FileOutputStream(String name,
boolean append)
throws FileNotFoundException
Creates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection.
First, if there is a security manager, its checkWrite method is called with name as its argument.
If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.
Parameters:
name - the system-dependent file name
append - if true, then bytes will be written to the end of the file rather than the beginning

-Peter
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!