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


Java CharArrayReader mark()用法及代碼示例

CharArrayReader類mark()方法

  • mark() 方法可在java.io包。
  • mark() 方法用於標記流中的當前位置,每當調用 reset() 方法時,它將流重置為最近調用 mark() 方法設置的位置。
  • mark() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
  • mark() 方法可能會在標記流時拋出異常。
    IOException:當給定的參數無效時,可能會拋出此異常。

用法:

    public void mark(int r_limit);

參數:

  • int r_limit– 表示在保留標記的同時可以讀取的字符數的限製。

返回值:

該方法的返回類型是void,它什麽都不返回。

例:

// Java program to demonstrate the example 
// of void mark(int r_limit) method of 
// CharArrayReader

import java.io.*;

public class MarkOfCAR {
 public static void main(String[] args) {

  CharArrayReader car_stm = null;
  char[] c_arr = {
   'a',
   'b',
   'c',
   'd'
  };

  try {
   // Instantiates CharArrayReader
   car_stm = new CharArrayReader(c_arr);

   // By using read() method isto 
   // read the character from car_stm
   int i1 = car_stm.read();
   int i2 = car_stm.read();
   int i3 = car_stm.read();
   int i4 = car_stm.read();

   System.out.println("i1:" + i1);

   // By using mark() method isto
   // set the current position in this
   // car_stm
   System.out.println("car_stm.mark(0):");
   car_stm.mark(0);
   System.out.println("i2:" + i2);
   System.out.println("i3:" + i3);

   // By using reset() method isto
   // reset the stream to the position 
   // set by the call mark() method
   System.out.println("car_stm.reset():");
   car_stm.reset();
   System.out.println("i2:" + i2);
   System.out.println("i3:" + i3);
   System.out.println("i4:" + i4);


  } catch (IOException e) {
   System.out.print("Stream closed!!!!");
  } finally {

   // Free all system resources linked
   // with the stream after closing
   // the stream
   if (car_stm != null)
    car_stm.close();
  }
 }
}

輸出

i1:97
car_stm.mark(0):
i2:98
i3:99
car_stm.reset():
i2:98
i3:99
i4:100


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java CharArrayReader mark() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。