RandomAccessFile类readUTF()方法 (RandomAccessFile Class readUTF() method)
readUTF() method is available in java.io package.
readUTF()方法在java.io包中可用。
readUTF() method is used to read this RandomAccessFile as a string.
readUTF()方法用于以字符串形式读取此RandomAccessFile。
readUTF() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
readUTF()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
readUTF() method may throw an exception at the time of reading the file as a string.
readUTF()方法在将文件读取为字符串时可能会引发异常。
- IOException: This exception may throw an exception while performing input/output operation.IOException :在执行输入/输出操作时,此异常可能会引发异常。
- EOFException: This exception may throw when the file pointer reaches EOF (End-Of-File) before reading all bytes.EOFException :当文件指针在读取所有字节之前到达EOF(文件末尾)时,可能引发此异常。
- UTFDataFormatException: This exception may throw when the bytes denote invalid UTF-8 encoding schemes.UTFDataFormatException :当字节表示无效的UTF-8编码方案时,可能引发此异常。
Syntax:
句法:
public final String readUTF();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of this method is String, it gets Unicode string value.
此方法的返回类型为String ,它获取Unicode字符串值。
Example:
例:
// Java program to demonstrate the example
// of String readUTF() method of
// RandomAccessFile
import java.io.*;
class RAFReadUTF {
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 to the file
ra_f.writeUTF("Welcome in Java World!!!");
// Initially set the file pointer
// is at 1 for reading the file
ra_f.seek(0);
// By using readUTF() method is to
// read a data in a string from
// this file
//String str = ra_f.readUTF();
System.out.println("ra_f.readUTF(): " + ra_f.readUTF());
// By using close() method isto
// close this stream ran_f
ra_f.close();
}
}
Output
输出量
ra_f.readUTF(): Welcome in Java World!!!
翻译自: https://www.includehelp.com/java/randomaccessfile-readutf-method-with-example.aspx