Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4707859

RandomAccessFile method to truncate from the start of a file

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P5
    • Resolution: Won't Fix
    • Affects Version/s: 1.4.0
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_2000

      Description

      Name: nt126004 Date: 06/25/2002


      FULL PRODUCT VERSION :
      C:\>java -version
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

      FULL OPERATING SYSTEM VERSION :

      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      In RandomAccessFile in the java.io package or FileChannel
      class in the java.nio.channels package there is no facility
      by which I can truncate the file beginning from the start.
      The trimming of the file by giving a lower size to the
      RandomAccessFile's setLength() or FileChannel's truncate()
      always does truncate from the end.

      But if I have a time series of data I want to be able to
      easily drop the first line of data and add another at the
      end. If it's provided in the API itself it would be cool.
      For example a boolean switch, to truncate from end or from
      the beginning.

      Thanks

      This bug can be reproduced often.

      ---------- BEGIN SOURCE ----------
      public class Test {

      public static void main(String [] args) {
      RandomAccessFile raf = null;
              try
              {
      raf = new RandomAccessFile("C:\\temp.txt","rw"); // ---
      > Hardcode any file you have
      long lengthOfFile = raf.length();
      if(lengthOfFile != 0) {

      raf.seek(0L);
      raf.readLine(); // ---> I want to delete this
      first line from input
      long getCurrentPointer = raf.getFilePointer();
      long offset = lengthOfFile - getCurrentPointer;

      System.err.println("Length == '"+lengthOfFile+"'");
      System.err.println("Offset == '"+getCurrentPointer+"'");
      System.err.println("New Length == '"+offset+"'");
      raf.setLength(offset);
      raf.seek(raf.length());
      }
      }
      catch(Exception e) {
      e.printStackTrace();
      }
      finally {
      try {
      raf.close();
      }
      catch(Exception e) {
      }
      }

      }
      }
      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Just code around this.
      (Review ID: 147171)
      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            alanb Alan Bateman
            Reporter:
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: