Class File


  • public class File
    extends Object
    A lightweight wrapper around RandomAccessFile that provides convenience methods for reading, writing, and navigating file content using intuitive helper operations.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int SEEK_CUR
      Origin constant used to move relative to the current file pointer.
      static int SEEK_END
      Origin constant used to move relative to the end of file.
      static int SEEK_SET
      Origin constant used to move relative to the beginning of file.
    • Constructor Summary

      Constructors 
      Constructor Description
      File()
      Constructor.
      File​(String filePath, String mode)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the underlying random access file if it is open.
      boolean endOfFile()
      Indicates whether the file pointer currently resides at end of file.
      void flush​(boolean metadata)
      Flushes file data to stable storage.
      int getc()
      Reads the next byte from the file.
      FileChannel getFileChannel()
      Provides direct access to the file channel backing this file wrapper.
      String getLine​(int separator)
      Reads a line terminated by the provided separator.
      String getLine​(int separator, int separator2)
      Reads a line terminated by either of the provided separators.
      void getLine​(StringBuilder stringBuilder, int separator)
      Reads characters into the provided builder until the separator or EOF is reached.
      void getLine​(StringBuilder stringBuilder, int separator, int separator2)
      Reads characters into the provided builder until one of the separators or EOF is reached.
      byte[] gets​(byte[] string, int offset, int maxCount)
      Reads up to maxCount bytes into the supplied array.
      String gets​(int maxLength)
      Reads up to maxLength bytes and returns them as a string.
      void ignore​(int nSymbols)
      Skips reading up to nSymbols bytes, stopping at EOF.
      void ignore​(int nSymbols, int delimiter)
      Skips reading up to nSymbols bytes or until the delimiter is encountered.
      long length()
      Retrieves the current length of the underlying file.
      boolean open​(String filePath, String mode)
      Opens the file at the specified path.
      String path()
      Returns the path used to open this file.
      int read​(byte[] buffer, int bufferLength)
      Reads up to bufferLength bytes into the provided array.
      void seek​(long offset)
      Moves the file pointer to an absolute offset.
      void seek​(long offset, int origin)
      Moves the file pointer relative to the specified origin.
      long tell()
      Reports the current file pointer location.
      String toString()
      Returns a human-readable representation of the file wrapper.
      void write​(byte value)
      Writes a single byte value to the file.
      void write​(byte[] buf)
      Writes the provided buffer to the file.
      void write​(byte[] buf, int offset, int length)
      Writes a slice of the provided buffer to the file.
      void write​(ByteBuffer value)
      Writes the contents of the provided ByteBuffer.
      void write​(String value)
      Writes the given string using the platform's default byte encoding.
    • Field Detail

      • SEEK_CUR

        public static final int SEEK_CUR
        Origin constant used to move relative to the current file pointer.
        See Also:
        Constant Field Values
      • SEEK_END

        public static final int SEEK_END
        Origin constant used to move relative to the end of file.
        See Also:
        Constant Field Values
      • SEEK_SET

        public static final int SEEK_SET
        Origin constant used to move relative to the beginning of file.
        See Also:
        Constant Field Values
    • Method Detail

      • close

        public void close()
                   throws IOException
        Closes the underlying random access file if it is open.
        Throws:
        IOException - if an I/O error occurs while closing the file
      • getFileChannel

        public FileChannel getFileChannel()
        Provides direct access to the file channel backing this file wrapper.
        Returns:
        the FileChannel associated with the underlying file
      • length

        public long length()
                    throws IOException
        Retrieves the current length of the underlying file.
        Returns:
        the file length in bytes
        Throws:
        IOException - if the length cannot be read
      • endOfFile

        public boolean endOfFile()
        Indicates whether the file pointer currently resides at end of file.
        Returns:
        true when the pointer is at EOF or an error occurs; false otherwise
      • path

        public String path()
        Returns the path used to open this file.
        Returns:
        the file path, or null if the file has not been opened
      • getc

        public int getc()
        Reads the next byte from the file.
        Returns:
        the unsigned byte value in the range 0-255, or EOF on failure or end of file
      • getLine

        public void getLine​(StringBuilder stringBuilder,
                            int separator)
                     throws IOException
        Reads characters into the provided builder until the separator or EOF is reached.
        Parameters:
        stringBuilder - the destination for the characters that are read
        separator - the byte value that stops the read
        Throws:
        IOException - if the read fails
      • getLine

        public String getLine​(int separator)
                       throws IOException
        Reads a line terminated by the provided separator.
        Parameters:
        separator - the byte value that terminates the line
        Returns:
        the collected characters (without the separator)
        Throws:
        IOException - if an I/O error occurs
      • getLine

        public void getLine​(StringBuilder stringBuilder,
                            int separator,
                            int separator2)
                     throws IOException
        Reads characters into the provided builder until one of the separators or EOF is reached.
        Parameters:
        stringBuilder - the destination for characters
        separator - the primary terminating character
        separator2 - an alternative terminating character
        Throws:
        IOException - if reading fails
      • getLine

        public String getLine​(int separator,
                              int separator2)
                       throws IOException
        Reads a line terminated by either of the provided separators.
        Parameters:
        separator - the primary terminating character
        separator2 - an alternative terminating character
        Returns:
        the collected characters without the separator byte
        Throws:
        IOException - if an I/O error occurs
      • gets

        public byte[] gets​(byte[] string,
                           int offset,
                           int maxCount)
        Reads up to maxCount bytes into the supplied array.
        Parameters:
        string - the destination buffer
        offset - the starting offset in the destination buffer
        maxCount - the maximum number of bytes to read
        Returns:
        the destination buffer, or null when EOF is reached or an error occurs
      • gets

        public String gets​(int maxLength)
        Reads up to maxLength bytes and returns them as a string.
        Parameters:
        maxLength - the maximum number of bytes to read
        Returns:
        a string containing the bytes read, or null when EOF is reached or an error occurs
      • ignore

        public void ignore​(int nSymbols,
                           int delimiter)
        Skips reading up to nSymbols bytes or until the delimiter is encountered.
        Parameters:
        nSymbols - the maximum number of bytes to skip
        delimiter - the terminating byte value; skipping stops when this is read
      • ignore

        public void ignore​(int nSymbols)
        Skips reading up to nSymbols bytes, stopping at EOF.
        Parameters:
        nSymbols - the maximum number of bytes to skip
      • flush

        public void flush​(boolean metadata)
                   throws IOException
        Flushes file data to stable storage.
        When metadata is true, both file data and metadata (e.g., file size) are forced to disk. When false, only file data is flushed.
        Parameters:
        metadata - whether file metadata should also be flushed
        Throws:
        IOException - if the flush operation fails
      • open

        public boolean open​(String filePath,
                            String mode)
        Opens the file at the specified path.
        Parameters:
        filePath - the target file path
        mode - the access mode used by RandomAccessFile
        Returns:
        true when the file is opened successfully; false otherwise
      • read

        public int read​(byte[] buffer,
                        int bufferLength)
                 throws IOException
        Reads up to bufferLength bytes into the provided array.
        Parameters:
        buffer - the destination buffer
        bufferLength - the maximum number of bytes to read
        Returns:
        the number of bytes read, or -1 at EOF
        Throws:
        IOException - if the read fails
      • seek

        public void seek​(long offset)
                  throws IOException
        Moves the file pointer to an absolute offset.
        Parameters:
        offset - the absolute position to move to
        Throws:
        IOException - if the seek fails
      • seek

        public void seek​(long offset,
                         int origin)
                  throws IOException
        Moves the file pointer relative to the specified origin.
        Parameters:
        offset - the number of bytes to move
        origin - one of SEEK_SET, SEEK_CUR, or SEEK_END
        Throws:
        IOException - if the seek fails
      • tell

        public long tell()
                  throws IOException
        Reports the current file pointer location.
        Returns:
        the zero-based offset of the file pointer
        Throws:
        IOException - if the position cannot be obtained
      • write

        public void write​(byte value)
                   throws IOException
        Writes a single byte value to the file.
        Parameters:
        value - the byte to write
        Throws:
        IOException - if the write fails
      • write

        public void write​(byte[] buf)
                   throws IOException
        Writes the provided buffer to the file.
        Parameters:
        buf - the bytes to write
        Throws:
        IOException - if the write fails
      • write

        public void write​(ByteBuffer value)
                   throws IOException
        Writes the contents of the provided ByteBuffer.
        Parameters:
        value - the buffer containing data to write
        Throws:
        IOException - if the write fails
      • write

        public void write​(String value)
                   throws IOException
        Writes the given string using the platform's default byte encoding.
        Parameters:
        value - the string to write
        Throws:
        IOException - if the write fails
      • write

        public void write​(byte[] buf,
                          int offset,
                          int length)
                   throws IOException
        Writes a slice of the provided buffer to the file.
        Parameters:
        buf - the source buffer
        offset - the starting index in the buffer
        length - the number of bytes to write
        Throws:
        IOException - if the write fails
      • toString

        public String toString()
        Returns a human-readable representation of the file wrapper.
        Overrides:
        toString in class Object
        Returns:
        a string including the path and closed state