RandomAccessFile in Java
The java.io.RandomAccessFile class provides reading and writing data at any location in the file. It is not part of the InputStream or OutputStream hierarchy. It is a completely separate class with its own (mostly native) methods. The explanation for this may be that RandomAccessFile has a very different behavior compared to other I/O classes, since it allows you to move back and forth within a file.
RandomAccessFile has specific methods such as:
- getFilePointer() to find the current location in the file;
- seek() to move to a new position in the file;
- length() to find out the file size;
- setLength() to set the file size;
- skipBytes() to try to skip a certain number of bytes;
- getChannel() to work with a unique file channel associated with a given file;
- methods for performing normal and formatted output from a file (read(), readInt(), readLine(), readUTF(), etc.);
- methods for writing or formatting directly to a file (write(), writeBoolean(), writeByte(), etc.).
It should also be noted that the RandomAccessFile constructors require a second argument that specifies the required mode of access to the file - read-only ("r"), read-write ("rw"), or some other kind.
File access modes in RandomAccessFile
- "r" opens the file read-only. Running any data writing methods will throw an IOException.
- "rw" opens a file for reading and writing. If the file has not yet been created, an attempt is made to create it.
- "rws" opens a file for reading and writing like "rw", but requires the system to synchronously write the changes to physical media whenever the file content or metadata changes.
- "rwd" opens a file for reading and writing like "rws", but requires the system to synchronously write changes to physical media only whenever the contents of the file change. If metadata changes, synchronous recording is not required.
Classes that support reading and writing compressed streams
- DeflaterOutputStream - data compression in the deflate format.
- Deflater - data compression into ZLIB format.
- ZipOutputStream is a descendant of DeflaterOutputStream for compressing data into Zip format.
- GZIPOutputStream is a descendant of DeflaterOutputStream for compressing data into GZIP format.
- InflaterInputStream - data decompression in deflate format.
- Inflater - ZLIB data decompression.
- ZipInputStream is a descendant of InflaterInputStream for decompressing Zip data.
- GZIPInputStream is a descendant of InflaterInputStream for decompressing GZIP data.
Redirection of standard I/O streams
The System class allows you to redirect standard input, output, and error output using a simple static method call:
- setIn(InputStream) - for input;
- setOut(PrintStream) - for output;
- setErr(PrintStream) - for displaying errors.
Delimiter character when specifying a path in the file system
The separator character is different for different operating systems. For Windows it is \, for Linux it is /.
In Java, you can get the separator for the current operating system by accessing the File.separator static field.
"absolute path" and "relative path"
An absolute (full) path is a path that points to the same location in the file system, regardless of the current working directory or other circumstances. The full path always starts at the root directory.
A relative path is a path relative to the current working directory of the user or active application.
Symbolic link
Symbolic link (also "symlink") is a special file in the file system, which, instead of user data, contains the path to the file that should be opened when trying to access this link (file). The target of a link can be any object: for example, another link, a file, a directory, or even a non-existent file (in the latter case, when you try to open it, you should receive a message that the file is missing).
Symbolic links are used to more conveniently organize the structure of files on a computer, because:
- allow for one file or directory to have several names and different attributes;
- are free from some of the restrictions inherent in hard links (the latter are valid only within the same file system (one partition) and cannot link to directories).
Read also:
Comments
Post a Comment