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


Java Java.io.CharArrayReader用法及代碼示例


CharArrayReader Class in Java

java.io.CharArrayReader 類使用字符數組創建字符緩衝區。

宣言:

public class CharArrayReader
   extends Reader

構造函數:

  • CharArrayReader(char[] char_array):從指定的字符數組創建CharArrayReader。
  • CharArrayReader(char[] char_array, int 偏移量, int maxlen):從字符數組的指定部分創建CharArrayReader。

方法:

  • read(): java.io.CharArrayReader.read()讀取單個字符,如果到達流末尾則返回 -1。
    句法:
public int read()
Parameters : 
-----------
Return  :
Returns read character as an integer ranging from range 0 to 65535.
-1 : when end of file is reached.
  • 讀取(char[] char_array, int 偏移量, int maxlen): java.io.CharArrayReader.read(char[] char_array, int 偏移量, int maxlen))讀取單個字符,如果到達流末尾則返回 -1
    句法:
public int read(char[] char_array, int offset, int maxlen))
Parameters : 
char_array : destination array  
offset : starting position from where to store characters
maxlen : maximum no. of characters to be read
Return  :
Returns all the characters read
-1 : when end of file is reached.
  • ready(): java.io.CharArrayReader.ready()檢查 Stream 是否準備好讀取。
    CharArrayReader 始終可供讀取。
    句法:
public boolean ready()
Parameters : 
-----------
Return  :
true if CharArrayReader is ready to be read.
  • 跳過(長字符):java.io.CharArrayReader.skip(長char_no)跳過‘char_no’號。的字符。如果 n 為負數,則此方法不執行任何操作並返回 0。
    句法:
public long skip(long char)
Parameters : 
char_no : char no. of characters to be skipped
Return  :
no. of characters skipped
Exception : 
IOException : In case of I/O error occurs

Java


// Java program illustrating the working of CharArrayReader class methods
// read(), skip(), ready()
// read(char[] char_array, int offset, int maxlen)
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        // Initializing the character array
        char[] geek = {'G', 'E', 'E', 'K', 'S'};
        // Initializing the char_array
        CharArrayReader char_array1 = new CharArrayReader(geek);
        CharArrayReader char_array2 = new CharArrayReader(geek);
        // Use of ready() method
        boolean check1 = char_array1.ready();
        if(check1 ==true)
            System.out.println("char_array1 is ready");
        else
            System.out.println("char_array1 is not ready");
        int a = 0;
        System.out.print("Use of read() method : ");
        // Use of read() method : reading each character one by one
        while((a = char_array1.read()) != -1)
        {
            char c1 = (char)a;
            System.out.println(c1);
            // Use of skip() method
            long char_no = char_array1.skip(1);
            System.out.println("Characters Skipped : "+(c1+1));
        }
        System.out.println("");
        // Use of ready() method
        boolean check2 = char_array2.ready();
        if(check2 ==true)
            System.out.println("char_array2 is ready");
        else
            System.out.println("char_array2 is not ready");
  // Use of read(char[] char_array, int offset, int maxlen) : reading a part of array
        char_array2.read(geek, 1, 2);
        int b = 0;
  System.out.print("Use of read(char[] char_array, int offset, int maxlen) method : ");
        while((b = char_array2.read()) != -1)
        {
            char c2 = (char)b;
            System.out.print(c2);
        }
    }
}

輸出:

char_array1 is ready
Use of read() method : G
Characters Skipped : 72
E
Characters Skipped : 70
S
Characters Skipped : 84

char_array2 is ready
Use of read(char[] char_array, int offset, int maxlen) method : EKS
  • 標記(int readLimit):java.io.CharArrayReader.mark(int readLimit)標記流中可以讀取字符的當前位置。此方法始終調用reset()方法。對 reset() 的後續調用會將流重新定位到這一點。
    句法:
public long mark(int readLimit)
Parameters : 
readLimit : No. of characters that can be read up to the mark
Return  :
void
Exception : 
IOException : In case of I/O error occurs
  • markSupported(): java.io.CharArrayReader.markSupported()指示流是否支持標記方法。
    句法:
public boolean markSupported()
Parameters : 
-------
Return  :
true if the mark method is supported by the stream
Exception : 
IOException : In case of I/O error occurs
  • reset(): java.io.CharArrayReader.reset()將流重置為最近的標記,如果從未標記過則重置為開頭。
    句法:
public void reset()
Parameters : 
-------
Return  :
void
Exception : 
IOException : In case of I/O error occurs
  • close(): java.io.CharArrayReader.close()關閉流並重新分配分配給它的資源。
    句法:
public void close()
Parameters : 
-------
Return  :
void
Exception : 
IOException : In case of I/O error occurs

Java


// Java program illustrating the working of FilterInputStream method
// mark(), reset()
// markSupported(), close()
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws Exception
    {
        // Initializing CharArrayReader
        CharArrayReader char_array = null;
        char[] geek = {'H', 'E', 'L', 'L', 'O', 'G',  'E',  'E',  'K', 'S'};
        try
        {
            char_array = new CharArrayReader(geek);
            // read() method : reading and printing Characters
            // one by one
            System.out.println("Char : "+(char)char_array.read());
            System.out.println("Char : "+(char)char_array.read());
            System.out.println("Char : "+(char)char_array.read());
            // mark() : read limiting the 'geek' input stream
            char_array.mark(0);
            System.out.println("mark() method comes to play");
            System.out.println("Char : "+(char)char_array.read());
            System.out.println("Char : "+(char)char_array.read());
            System.out.println("Char : "+(char)char_array.read());
            // Use of markSupported() :
            boolean check = char_array.markSupported();
            if (check == true )
                System.out.println("mark() supported\n");
            if (char_array.markSupported())
            {
                // reset() method : repositioning the stream to
                // marked positions.
                char_array.reset();
                System.out.println("reset() invoked");
                System.out.println("Char : "+(char)char_array.read());
                System.out.println("Char : "+(char)char_array.read());
            }
            else
                System.out.println("mark() method not supported.");
             
        }
        catch(Exception excpt)
        {
            // in case of I/O error
            excpt.printStackTrace();
        }
        finally
        {
            // Use of close() : releasing the resources back to the
            // GarbageCollector when closes
            if(char_array != null)
                char_array.close();
        }
    }
}

輸出:

Char : H
Char : E
Char : L
mark() method comes to play
Char : L
Char : O
Char : G
mark() supported

reset() invoked
Char : L
Char : O


相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.CharArrayReader Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。