首頁 > 基礎教程 > IO流 > InputStream類
Java InputStream.mark()設定輸入流的目前位置
定義
public void mark(int readlimit)
參數
readlimit - 可讀取的最大位元組數。
傳回
不傳回任何值
異常
IOException:I/O 錯誤
執行個體
public static void main(String[] args) throws Exception
{
InputStream is = null;
try
{
is = new FileInputStream("C://51gjie.txt");
System.out.println("Char : " + (char) is.read());
is.mark(0);//設定流位置重新為0
System.out.println("Char : " + (char) is.read());
if(is.markSupported())
{
is.reset();
System.out.println("Char : " + (char) is.read());
}
}
catch(Exception e)
{}
finally
{
if(is != null) is.close();
}
}
1. InputStream的mark方法不執行任何操作,隻是标記輸入流的位置,如果要重新定位到這個位置,需要調用reset()方法才行。
2. 如果方法markSupported傳回true,則流将以某種方式記住在調用mark之後讀取的所有位元組,并準備在每次調用方法reset時再次提供相同的位元組。 但是,如果在調用複位之前從流中讀取了超過readlimit個位元組的資料,則該流根本不需要記住任何資料。
版權聲明:本文為JAVASCHOOL原創文章,未經本站允許不得轉載。