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.

Recommended Answers

All 7 Replies

One suggestion is to read the entire file and then write again what you want at the first line and the write the rest of the data read

One suggestion is to read the entire file and then write again what you want at the first line and the write the rest of the data read

Well I also thought but the thing is there are approximately 10000 lines of data, and each line containing 500 bytes of data . Again reading and writing file not be a efficient way. Isn't there any other option of doing that

Well I also thought but the thing is there are approximately 10000 lines of data, and each line containing 500 bytes of data . Again reading and writing file not be a efficient way. Isn't there any other option of doing that

I have never had to do that, but I believe there are some classes that enable you to do that. If you check the java.io API you might find something

After searching for examples on the net, I found this:
Assuming your file is like this:

XXXX
aaaaa
bbbbb

And 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
bbbbb

But if you write this: fc.write(ByteBuffer.wrap("1234567".getBytes())); You will get this:

1234567aaa
bbbbb

After searching for examples on the net, I found this:
Assuming your file is like this:

And 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:


But if you write this: fc.write(ByteBuffer.wrap("1234567".getBytes())); You will get this:

Thanks a lot for your suggestion. I also tried this and geting this abnormal behavior. I dont know whats the reason. Can you suggest a reason and how to rectify it . Actually I am writing a file with a blank line in its start, and then counting the no of line written afterward. I have to write an integer,i.e. the no of lines written in the file, in that blank line.Initially I was just writing the newline escape character("\n"). Due to this my next line was getting edited instead of inserting that in blank space. But then I tried to give some space and some tabs at beginning it will write that some no of digits. But if the no is too big it will again overwrite my data......
How to overcome this????

Thanks a lot for your suggestion. I also tried this and geting this abnormal behavior. I dont know whats the reason. Can you suggest a reason and how to rectify it . Actually I am writing a file with a blank line in its start, and then counting the no of line written afterward. I have to write an integer,i.e. the no of lines written in the file, in that blank line.Initially I was just writing the newline escape character("\n"). Due to this my next line was getting edited instead of inserting that in blank space. But then I tried to give some space and some tabs at beginning it will write that some no of digits. But if the no is too big it will again overwrite my data......
How to overcome this????

I don't know, never done anything like that before. You could try to put many spaces at the first line of the file.

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.