Advertisement
raffaele181188

Java remove line from text file

Sep 29th, 2012
1,234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package com.zybnet;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.RandomAccessFile;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13.  
  14. import junit.framework.TestCase;
  15.  
  16. public class RemoveLine extends TestCase {
  17.  
  18.     /**
  19.      * @param args args[0] is the filename, args[1] is the line number
  20.      * @throws IOException
  21.      */
  22.     public static void main(String[] args) throws IOException {
  23.         // Use a random access file
  24.         RandomAccessFile file = new RandomAccessFile(args[0], "rw");
  25.         int counter = 0, target = Integer.parseInt(args[1]);
  26.         long offset = 0, length = 0;
  27.        
  28.         // Find offset and length of target line
  29.         while (file.readLine() != null) {
  30.             counter++;
  31.             if (counter == target)
  32.                 break;
  33.             offset = file.getFilePointer();
  34.         }
  35.        
  36.         length = file.getFilePointer() - offset;
  37.        
  38.         if (target > counter) {
  39.             file.close();
  40.             throw new IOException("No such line!");
  41.         }
  42.        
  43.         byte[] buffer = new byte[4096];
  44.         // the file pointer is set at the end of the target line
  45.         int read = -1;
  46.         while ((read = file.read(buffer)) > -1){
  47.             // go back
  48.             file.seek(file.getFilePointer() - read - length);
  49.             file.write(buffer, 0, read);
  50.             // go ahead by length
  51.             file.seek(file.getFilePointer() + length);
  52.         }
  53.         file.setLength(file.length() - length);
  54.         file.close();
  55.     }
  56.    
  57.     public static void testMain() throws IOException {
  58.         String[] lines = {"Lorem", "Ipsum", "Dolor", "Sit", "Amen"};
  59.         String tmpFile = "/tmp/a";
  60.         int index = 1;
  61.         BufferedWriter out = new BufferedWriter(new FileWriter(tmpFile));
  62.         for (String line : lines) {
  63.             out.write(line);
  64.             out.write("\n");
  65.         }
  66.         out.close();
  67.         main(new String[] {tmpFile, Integer.toString(index)});
  68.         List<String> read = new ArrayList<String>(lines.length);
  69.         String line = null;
  70.         BufferedReader in = new BufferedReader(new FileReader(tmpFile));
  71.         while ((line = in.readLine()) != null) {
  72.             read.add(line);
  73.             System.err.println(line);
  74.         }
  75.         in.close();
  76.        
  77.         List<String> src = new LinkedList<String>(Arrays.asList(lines)) ;
  78.         src.remove(index - 1);
  79.         assertEquals(src, read);
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement