当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。