I have a file which is already written till some lines. I have a blank line at the start of the file and I want to write in that line using Java.
I thought creating a FileWriter object by using FileWriter(filename, true) and then a BufferedWriter object will start writing from top. But its appending at the end of file. I dont want to destroy my data of file already written. How can I do that.
gunjannigam
28
Junior Poster
Recommended Answers
Jump to PostI think that the RandomAccessFile might help you.
Jump to PostAfter searching for examples on the net, I found this:
Assuming your file is like this:XXXX
aaaaa
bbbbbAnd you run this:
RandomAccessFile raf = new RandomAccessFile("fileName","rw"); FileChannel fc = raf.getChannel(); fc.position(0); fc.write(ByteBuffer.wrap("1234".getBytes())); fc.close(); raf.close();
You will get this:
1234
aaaaa
bbbbbBut if you write …
All 7 Replies
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
gunjannigam
28
Junior Poster
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
gunjannigam
28
Junior Poster
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.