RandomAccessFile类getChannel()方法
- getChannel() 方法可在
java.io
包。 - getChannel() 方法用于获取与此 RandomAccessFile 流链接的不同 FileChannel。
- getChannel() 方法是一个非静态方法,它只能通过类对象访问,如果我们尝试使用类名访问方法,那么我们将得到一个错误。
- getChannel() 方法返回文件通道时不抛出异常。
用法:
public final FileChannel getChannel();
参数:
- 它不接受任何参数。
返回值:
这个方法的返回类型是FileChannel
,它获取与此 RandomAccessFile 相关联的 FileChannel 对象。
例:
// Java program to demonstrate the example
// of final FileChannel getChannel() method of
// RandomAccessFile
import java.io.*;
public class RAFGetChannel {
public static void main(String[] args) throws Exception {
// Instantiate a random access file
// object with file name and permissions
RandomAccessFile ra_f = new RandomAccessFile("e:/includehelp.txt", "rw");
// By using writeUTF() method is to
// write data in the file
ra_f.writeUTF("Welcome, in Java World!!!");
// Initially set the file pointer
// is at 0 for reading the file
ra_f.seek(0);
// To read file content by using
// readUTF()
System.out.println("ra_f .readUTF():" + ra_f.readUTF());
// By using getChannel() method is to
// return the channel linked with this
// object
System.out.println("ra_f.getChannel():" + ra_f.getChannel());
// By using close() method isto
// close this stream ran_f
ra_f.close();
}
}
输出
E:\Programs>java RAFGetChannel ra_f .readUTF():Welcome, in Java World!!! ra_f.getChannel():[email protected]
相关用法
- Java RandomAccessFile getFD()用法及代码示例
- Java RandomAccessFile getFilePointer()用法及代码示例
- Java RandomAccessFile close()用法及代码示例
- Java RandomAccessFile readByte()用法及代码示例
- Java RandomAccessFile readChar()用法及代码示例
- Java RandomAccessFile writeUTF()用法及代码示例
- Java RandomAccessFile readUnsignedByte()用法及代码示例
- Java RandomAccessFile writeFloat()用法及代码示例
- Java RandomAccessFile writeChar()用法及代码示例
- Java RandomAccessFile writeBytes()用法及代码示例
- Java RandomAccessFile readInt()用法及代码示例
- Java RandomAccessFile readShort()用法及代码示例
- Java RandomAccessFile seek()用法及代码示例
- Java RandomAccessFile skipBytes()用法及代码示例
- Java RandomAccessFile writeDouble()用法及代码示例
- Java RandomAccessFile readDouble()用法及代码示例
- Java RandomAccessFile readLong()用法及代码示例
- Java RandomAccessFile writeInt()用法及代码示例
- Java RandomAccessFile readUTF()用法及代码示例
- Java RandomAccessFile writeBoolean()用法及代码示例
注:本文由纯净天空筛选整理自Preeti Jain大神的英文原创作品 Java RandomAccessFile getChannel() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。