描述
這個java.io.ObjectInputStream.readUTF()方法讀取修改後的 UTF-8 格式的字符串。
聲明
以下是聲明java.io.ObjectInputStream.readUTF()方法。
public String readUTF()
參數
NA
返回值
此方法返回字符串。
異常
UTFDataFormatException− 如果讀取的字節不代表字符串的有效修改 UTF-8 編碼。
IOException− 如果從底層 InputStream 讀取時出現 I/O 錯誤。
示例
下麵的例子展示了使用java.io.ObjectInputStream.readUnsignedShort()方法。
package com.tutorialspoint;
import java.io.*;
public class ObjectInputStreamDemo {
public static void main(String[] args) {
String s = "Hello World!";
try {
// create a new file with an ObjectOutputStream
FileOutputStream out = new FileOutputStream("test.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
// write something in the file
oout.writeUTF(s);
oout.writeUTF("This is an example");
oout.flush();
// create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));
// read and print the string
System.out.println("" + ois.readUTF());
// read and print the string
System.out.println("" + ois.readUTF());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
讓我們編譯並運行上麵的程序,這將產生以下結果 -
Hello World! This is an example
相關用法
- Java Java.io.ObjectInputStream.readUnshared()用法及代碼示例
- Java Java.io.ObjectInputStream.readObject()用法及代碼示例
- Java Java.io.ObjectInputStream.readInt()用法及代碼示例
- Java Java.io.ObjectInputStream.readFloat()用法及代碼示例
- Java Java.io.ObjectInputStream.readChar()用法及代碼示例
- Java Java.io.ObjectInputStream.readBoolean()用法及代碼示例
- Java Java.io.ObjectInputStream.read()用法及代碼示例
- Java Java.io.ObjectInputStream.readLong()用法及代碼示例
- Java Java.io.ObjectInputStream.readDouble()用法及代碼示例
- Java Java.io.ObjectInputStream.readFields()用法及代碼示例
- Java Java.io.ObjectInputStream.readShort()用法及代碼示例
- Java Java.io.ObjectInputStream.readFully()用法及代碼示例
- Java Java.io.ObjectInputStream.readByte()用法及代碼示例
- Java Java.io.ObjectInputStream.resolveClass()用法及代碼示例
- Java Java.io.ObjectInputStream.resolveObject()用法及代碼示例
- Java Java.io.ObjectInputStream.getField.get()用法及代碼示例
- Java Java.io.ObjectInputStream.skipBytes()用法及代碼示例
- Java Java.io.ObjectInputStream.available()用法及代碼示例
- Java Java.io.ObjectInputStream.close()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeObject()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.io.ObjectInputStream.readUTF() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。