Remove data from file

cougar2002 used Ask the Experts™
I would like to know how to remove a specific line of data from the file



I want to remove data2. How to do that without leaving a blank line? Do I have to read and write all to an array and than write back to file?
Pls assist.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Yes you have to read it all, and write required data back to to file.
yup, i think you need to read and write to an array, then write the your destination file again.


Is there any method where I can just remove that line away?
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Mick BarryJava Developer
Top Expert 2010


if you want - you can replace (for example) the line "data3" with spaces - and ignore such lines later, but the result of this could be a large file, containig only few meaningful lines.


but if i were to just change the dat in certain line, how to do that?


data1 data2 data3 data4
data5 data6 data7 data8
data9 data10 data11 data12

I want to change data7 form 37.20 to 28.20
how do i do that???

you can do it only if the new data is with the same size (in bytes) as the original. to do this - use RandomAccessFile and seek() before writing

FYI: This is not just necessary in Java, its necessary in all languages/editors/etc. Some may handle the details behind the scenes, but it's essentially what is going on.

Computer files are not accordians.


Sori but i do'nt really understand how to do that..any example??

How you do this depends how large the file is and if you have control of the file format. If you let us know which of the following options best meet your needs, maybe someone can provide an example.

1) If you know the max size of each piece of data, you can originally create the file where each field is a fixed width. For instance, you have a field that will not be more than 30 characters. You place the current data and then add exactly enough blank bytes to make 30 characters, You then start the next field. This is probably the way to go if you plan on updating the file a lot and/or you have control over the format. Many databases use this technique.

2) If the file is fairly small, you can read the whole file into memory, make the necessary changes, and then write the whole file. Obviously, the file would have to be fairly small for this to be feasible.

3) If the file is large you will have to read, change, and then write to a different file blocks at a time. You would read a section of data, say a line, make necessary changes to that line, then write to a different file.


Basically, the file format is like this;

123331 111141 12.00 address

They are all seperated by a space between them.
The 1st three data/s size is fixed.
I wanted to change the 3rd data.
The file size is also very small.(ard 30 lines of data)

You can use a fixed length lines, by appending for example ' ' at the end of each line until reaching the fixed length.


I use this isist??
public void write(byte[] b,
                  int off,
                  int len)

b[] -> the data to b written
off->the location
len->the length to be written??
if you use a fixed length lines (or even - you can use fixed length fields) - you can use RandomAccess file:
RandomAccessFile file = new RandomAccessFile("file name"); * lineLength);
afther that use the same write() method to overwrite the data at this position

To use #1 (above), all the data must be a fixed width. If all the data is fixed width (including the last line) or if the whole line is a given length with delimiters (your spaces), go with RandomAccessFile (as Venci75 suggested). If not you will have to use #2 or #3.

Here's a trivial implementation of #3. It just grabs the old value from the file and appends a "1" onto it and then writes the line in the new file. It could have typos or whatever.

BufferedReader in = new BufferedReader(new FileReader(file));
PrintWriter out = new PrintWriter(new FileWriter(file2));
String line;
while ((line = in.readLine() != null) {
  int index = line.lastIndexOf(' ')+1;
  String oldVal = line.substring(index);
  out.println(line.substring(0,index) + oldVal + "1");  

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial