java.io.DataInputStream.readUTF(DataInput in) 方法讀取在已使用UTF-8修改版格式編碼的字串。
以下是java.io.DataInputStream.readUTF(DataInput in) 方法宣告:
public static final String readUTF(DataInput in)
NA
此方法返回一個= Unicode字串
EOFException
UTFDataFormatException
IOException -- 如果發生I/ O錯誤..
下面的例子顯示java.io.DataInputStream.readUTF(DataInput in)方法的用法。
package com.yiibai; import java.io.FileOutputStream; import java.io.IOException; public class FileOutputStreamAvailable extends FileOutputStream { public FileOutputStreamAvailable() throws Exception { super("C://test.txt"); } public static void main(String[] args) throws IOException { FileOutputStream fos = null; FileOutputStreamAvailable fosa = null; try{ // create new File input stream fosa = new FileOutputStreamAvailable(); // read byte from file input stream fosa.finalize(); // converts int to char System.out.println("Stream is closed successfully."); }catch(Exception ex){ // if any error occurs ex.printStackTrace(); }finally{ // releases all system resources from the streams if(fos!=null) fos.close(); } } }
假設我們有一個文字檔案c:/ test.txt,它具有以下內容。該檔案將被用作輸入到我們的範例程式:
ABCDE
讓我們編譯和執行上面的程式,這將產生以下結果:
Stream is closed successfully.