codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Appending to a file  aktuxer at 21:53 on Sunday, December 10, 2006
 

Hello all,
I'm currently writing a java program that allows a user to input a name that is then saved to a .txt file. It works fine if I run the program once but when I run the program again it adds the data to the same line. Is there a way I can make the program add the names to a new line of text in the .txt file. My program has multiple classes, belows is only the one that actually writes to to the file. Thanks in advance



public class MyAppend
{



public void MyDisplay()
{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String userName = null;


try {
userName = br.readLine();
//this is where the user inputs his name
} catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
String Name = userName;
//this is what the input will be known as to the program

try
{
PrintWriter out = new PrintWriter(
new FileOutputStream("/forum/thetext.txt", true));
out.print(Name);
out.close();
//here the program saves the name to the text file. I either want it to start a new line everytime it writes something
}
catch (IOException e)
{
System.out.println(e);
}


}
[/qoute]

  Re: Appending to a file  crwood at 04:42 on Tuesday, December 12, 2006
 

Two choices:
1 - read the entire file, saving what you read, add the name as desired and then write it all back to the file. Every time you open a file and write to it the writing starts at the beginning and writes over all that is there.
2 - use a RandomAccessFile in which you can preset the amount of space available for multiple entries. Then access the desired space and overwrite it.

  Re: Appending to a file  mmarab at 08:39 on Monday, December 18, 2006
 

Try:

new FileOutputStream("thetext.txt\n", true));

Hope it works.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: need help!!!
•  Connecting to an https server using NNTP perl
•  Is there an ADMIN!!!!! Is this place maintained!
•  small question
•  Re: Text to Speech Converter
•  Re: Multithreading question
•  Re: This forum sucks.
•  Re: Appending to a file
•  Re: Problems with mkdir


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2006