Class CloseableRandomAccessFile

  • All Implemented Interfaces:
    java.io.Closeable, java.io.DataInput, java.io.DataOutput, java.lang.AutoCloseable, java.nio.channels.ByteChannel, java.nio.channels.Channel, java.nio.channels.ReadableByteChannel, java.nio.channels.SeekableByteChannel, java.nio.channels.WritableByteChannel

    public class CloseableRandomAccessFile
    extends FileAccess
    RandomAccessFile-ähnliche implementierung, die es erlaubt, die Datei zu schließen und bei Bedarf wieder von der selben Position zu öffnen. Diese Implementierung kann verwendet werden, weil eine Applikation sonst viele Dateien gleichzeitig öffnet um Beschränkungen im Betriebssystem zu umgehen.

    Zusätzlich ist die Ein- und Ausgabe gepuffert, wodurch die Performance wesentlich erhöht wird.

    Es sollte sichergestellt werden, dass kein anderer Prozess parallel die Datei bearbeitet, sonst entsteht möglicherweise unerwünschtes Verhalten.

    • Constructor Detail

      • CloseableRandomAccessFile

        public CloseableRandomAccessFile​(java.io.File file)
      • CloseableRandomAccessFile

        public CloseableRandomAccessFile​(java.io.File file,
                                         int bufferSize)
    • Method Detail

      • getDataOutStream

        protected java.io.DataOutputStream getDataOutStream()
                                                     throws java.io.IOException
        Description copied from class: FileAccess
        Gibt einen DataOutputStream zum Schreiben zurück
        Specified by:
        getDataOutStream in class FileAccess
        Returns:
        DataOutputStream
        Throws:
        java.io.IOException
      • getDataInStream

        protected java.io.DataInputStream getDataInStream()
                                                   throws java.io.IOException
        Description copied from class: FileAccess
        Gibt einen DataInputStream zum Lesen zurück
        Specified by:
        getDataInStream in class FileAccess
        Returns:
        DataInputStream
        Throws:
        java.io.IOException
      • getChannel

        protected final java.nio.channels.FileChannel getChannel()
                                                          throws java.io.IOException
        Description copied from class: FileAccess
        Gibt einen gültigen FileChannel zurück, mit dem die Klasse die Datei manipulieren kann.
        Specified by:
        getChannel in class FileAccess
        Returns:
        einen gültigen FileChannel
        Throws:
        java.io.IOException
      • openFile

        protected void openFile()
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • isOpen

        public boolean isOpen()
        Specified by:
        isOpen in interface java.nio.channels.Channel
        Specified by:
        isOpen in class FileAccess
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class FileAccess
        Throws:
        java.io.IOException
      • closeFile

        public void closeFile()
                       throws java.io.IOException
        Schließt die Datei auf Betriebssystemebene, aber erlaubt weitere Lese- und Schreibzugriffe. Falls später wieder Daten gelesen oder geschrieben werden sollen wird die Datei automatisch erneut geöffnet. Da nicht sichergestellt wird, dass die Datei nicht zwischenzeitlich manipuliert wird muss der Anwender hier vorsichtig sein.
        Throws:
        java.io.IOException