views:

308

answers:

4

I want to read a file in java. And then, I want to delete a line from that file without the file being re-written.

How can I do this?

Someone suggested me to read/write to a file without the file being re-written with the help of RandomAccessFile. http://stackoverflow.com/questions/1984534/how-to-write-data-to-a-file-through-java

Specifically, that files contains lines. One line contains three field - id, name and profession - separated by \t. I want to read that file through a Reader or InputStream or any other way and then search for a line that has the specified keyword (say 121) and then wants to delete that whole line.

This operation needs to be performed without the whole file being re-written

+4  A: 

I don't think you can alter a file on a filesystem in any way without writing to it, including deleting a line.

Do you mean you want to write the file without altering the file's metadata, like the last modified time?


Based on your updated question:

I don't think you can do what you're asking to do here. You can't remove bytes from a file once the file has been written, note no deleteByte or removeByte methods in RandomAccessFile.

I suggest moving the content of your file to a database - that allows this kind of record-oriented operation.

The alternative is, you have to rewrite the file. Sorry!

Brabster
+2  A: 

"Lines" are an abstract concept -- they're just an arbitrary sequence of bytes terminated by "\n". BufferedWriters and their ilk don't support textual editing in this way, so you'll have to rewrite the file in its entirety.

In general, what you want to do is:

  • open a reader
  • read content into some suitable data structure
  • close the reader
  • change data/records which need to be changed in this data structure
  • open a FileWriter with append == false
  • write content of data structure to resulting file
  • close FileWriter
John Feminella
A: 

add a marker in your lines saying if your line is deleted or not : this will make a software delete instead od a hardware delete.

if you have to insert new lines, you then can reuse those that are marked as deleted.

chburd
A: 

Hi, The below code searchs the line or fields in a single text file reads the file line by line then the line or fields can be replaced by " " or any other string. Here we use the pattern and Matcher classes.

If this not clearing your question do let me know.

import java.io.; import java.util.regex.; import java.util.Properties; public class DeleteLine {

public static void main(String[] args) { BufferedReader br = null; try { String line=null; File f = new File("d:/xyz.txt");

String replaceString=properties.getProperty("replaceAll.String");

; br = new BufferedReader(new FileReader("d:/giri/scjp/");

          while ( (line = br.readLine()) != null )//BufferedReader contains readline method
  {

      Pattern p=Pattern.compile(searchString);/*here u an specify the line u want to delete */
      Matcher m=p.matcher(line);
      line=m.replaceAll(replaceString);/*here replace String u can " " so that it will be emptied */
      System.out.println(line);
    } 
      //System.out.println(line);
           } 

      }

} br = new BufferedReader(new FileReader("d:/xyz.txt")); String line = null;

}

catch (FileNotFoundException e)
{
    System.out.println("File couldnt find");

  e.printStackTrace();
}
catch (IOException e)
{
  e.printStackTrace();
}

}

}

giri
Readability suggestion: "Indent [at least] four spaces to create an escaped <pre><code> block." http://stackoverflow.com/editing-help
trashgod