Results 1 to 1 of 1
Thread: Writing to a file (at the end)
-
Writing to a file (at the end)
Appeding text to text files is a common operation. But for that, you wish to move the file pointer at the end of the file. The code snippet below does exactly that.
Java Code:public class appendtext { public static void main(String args[]){ try { PrintStream out = new PrintStream(new AppendFileStream("myfile")); out.print("A new line of text"); out.close(); } catch(Exception e) { System.out.println(e.toString()); } } } class AppendFileStream extends OutputStream { RandomAccessFile fd; public AppendFileStream(String file) throws IOException { fd = new RandomAccessFile(file,"rw"); fd.seek(fd.length()); } public void close() throws IOException { fd.close(); } public void write(byte[] b) throws IOException { fd.write(b); } public void write(byte[] b,int off,int len) throws IOException { fd.write(b,off,len); } public void write(int b) throws IOException { fd.write(b); } }
Similar Threads
-
writing to a excel file from java program
By priyankabhar in forum New To JavaReplies: 6Last Post: 03-15-2012, 04:29 PM -
writing and reading unicode characters from a file
By ranoosh in forum Advanced JavaReplies: 4Last Post: 09-28-2008, 04:34 AM -
Writing UTF to file using writeUTF
By Java Tip in forum Java TipReplies: 0Last Post: 01-22-2008, 08:19 PM -
writing to a file
By bugger in forum New To JavaReplies: 1Last Post: 11-11-2007, 02:49 AM -
Help with File reading and writing
By baltimore in forum New To JavaReplies: 1Last Post: 07-31-2007, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks