當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.io.DataInputStream.readUTF()用法及代碼示例


描述

這個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.tutorialspoint;

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 {
      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.

相關用法


注:本文由純淨天空篩選整理自 Java.io.DataInputStream.readUTF() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。