getChannel(): This method is available in the java.io.RandomAccessFile class of Java.
Syntax:
FileChannel java.io.RandomAccessFile.getChannel()
This method returns the unique FileChannel object associated with this file.
The position of the returned channel will always be equal to this object's file-pointer offset as returned by the getFilePointer method.
Parameters: NA
Returns: the file channel associated with this file.
Exceptions: NA
Approach
Java
import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;public class RandomAccessFilegetChannel {public static void main(String[] args) throws IOException {File file = new File("D:\\hello.txt");String mode = "r";RandomAccessFile randomAccessFile =new RandomAccessFile(file, mode);System.out.println(randomAccessFile.getChannel());randomAccessFile.close();}}
Output:
sun.nio.ch.FileChannelImpl@4d591d15
Some other methods of RandomAccessFile
close(): This method closes this random access file stream and releases any system resources associated with the stream.
getChannel(): This method returns the unique FileChannel object associated with this file.
getFD(): This method returns the opaque file descriptor object associated with this stream.
getFilePointer(): This method returns the current offset in this file.
length(): This method returns the length of this file.
RandomAccessFile(File, String): This method creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
RandomAccessFile(String, String): This method creates a random access file stream to read from, and optionally to write to, a file with the specified name.
read(): This method reads a byte of data from this file.
read(byte[]): This method reads up to b.length bytes of data from this file into an array of bytes.
read(byte[], int, int): This method reads up to len bytes of data from this file into an array of bytes.
readBoolean(): This method reads a single byte from the file, starting at the current file pointer.
readByte(): This method reads a signed eight-bit value from this file.
readChar(): This method reads a character from this file. This method reads two bytes from the file, starting at the current file pointer.
readDouble(): This method reads a long value, starting at the current file pointer, as if by the readLong method, and then converts that long to a double using the longBitsToDouble method in class Double.
readFloat(): This method reads an int value, starting at the current file pointer, as if by the readInt method, and then converts that int to a float using the intBitsToFloat method in class Float.
readFully(byte[]): This method reads b.length bytes from this file into the byte array, starting at the current file pointer.
readFully(byte[], int, int): This method reads exactly the len bytes from this file into the byte array, starting at the current file pointer.
readInt(): This method reads a signed 32-bit integer from this file. This method reads 4 bytes from the file, starting at the current file pointer.
readLine(): This method reads the next line of text from this file.
readLong(): This method reads a signed 64-bit integer from this file. This method reads eight bytes from the file, starting at the current file pointer.
readShort(): This method reads a signed 16-bit number from this file. The method reads two bytes from this file, starting at the current file pointer.
readUnsignedByte(): This method reads an unsigned eight-bit number from this file. This method reads a byte from this file, starting at the current file pointer, and returns that byte.
readUnsignedShort(): This method reads an unsigned 16-bit number from this file. This method reads two bytes from the file, starting at the current file pointer.
readUTF(): This method reads in a string from this file. The string has been encoded using a modified UTF-8 format.
seek(long): This method sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
setLength(long): This method sets the length of this file.
skipBytes(int): This method attempts to skip over n bytes of input discarding the skipped bytes.
write(byte[]): This method writes b.length bytes from the specified byte array to this file, starting at the current file pointer.
write(int): This method writes the specified byte to this file.
write(byte[], int, int): This method writes len bytes from the specified byte array starting at offset off to this file.
writeBoolean(boolean): This method writes a boolean to the file as a one-byte value.
writeByte(int): This method writes a byte to the file as a one-byte value.
writeBytes(String): This method writes the string to the file as a sequence of bytes.
writeChar(int v): This method writes a char to the file as a two-byte value, high byte first.
writeChars(String): This method writes a string to the file as a sequence of characters.
writeDouble(double): This method converts the double argument to a long using the doubleToLongBits method in class Double and then writes that long value to the file as an eight-byte quantity, high byte first.
writeFloat(float): This method converts the float argument to an int using the floatToIntBits method in class Float and then writes that int value to the file as a four-byte quantity, high byte first.
writeInt(int): This method writes an int to the file as four bytes, the high byte first.
writeLong(long): This method writes a long to the file as eight bytes, high byte first.
writeShort(int): This method writes a short to the file as two bytes, the high byte first.
writeUTF(String): This method writes a string to the file using modified UTF-8 encoding in a machine-independent manner.
No comments:
Post a Comment