Home arrow static arrow Java Programming [Archive] - adding to the end of a file
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - adding to the end of a file
This topic has 3 replies on 1 page.

Posts:38
Registered: 5/28/04
adding to the end of a file  
Aug 9, 2004 4:46 AM



 
hi guys,
im tryin to open a file then add changes to the end of it, at the mo im using the following but it doesnt add to the end, any ideas?

try {
BufferedReader br = new BufferedReader(new FileReader("H:
new folder
TransactionFile.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("H:
new folder
TransactionFile.txt"));
while (br.readLine() != null) {
bw.newLine();
}
bw.write("Transaction started at: " + (new Date()));
bw.close();
br.close();
}
catch(Exception e){
System.out.println(e);
}
 

Posts:575
Registered: 98-01-01
Re: adding to the end of a file  
Aug 9, 2004 5:59 AM (reply 1 of 3)



 
Use the FileWriter constructor that accepts a boolean parameter that determines whether to append to the file. For example:

BufferedWriter bw = new BufferedWriter(new FileWriter("H:/new folder/TransactionFile.txt",true));
 

Posts:3,258
Registered: 00-08-28
Re: adding to the end of a file  
Aug 9, 2004 8:57 AM (reply 2 of 3)



 
another option is RandomAccessFile

RandomAccessFile raf = new RandomAccessFile("OutputFile.txt", "rw"); raf.seek(raf.length());  // Jump to EOF. ... raf.close();
 

Posts:3,258
Registered: 00-08-28
Re: adding to the end of a file  
Aug 9, 2004 8:58 AM (reply 3 of 3)



 
also try using format tags when posting code makes it easier for the reader.

%-)
 
This topic has 3 replies on 1 page.