Java的字符讀寫類CharArrayReader和CharArrayWriter應用示例。本站提示廣大學習愛好者:(Java的字符讀寫類CharArrayReader和CharArrayWriter應用示例)文章只能為提供參考,不一定能成為您想要的結果。以下是Java的字符讀寫類CharArrayReader和CharArrayWriter應用示例正文
CharArrayReader
CharArrayReader 是字符數組輸出流。它和ByteArrayInputStream相似,只不外ByteArrayInputStream是字節數組輸出流,而CharArray是字符數組輸出流。CharArrayReader 是用於讀取字符數組,它繼續於Reader。操作的數據是以字符為單元!
CharArrayReader 函數列表:
CharArrayReader(char[] buf) CharArrayReader(char[] buf, int offset, int length) void close() void mark(int readLimit) boolean markSupported() int read() int read(char[] buffer, int offset, int len) boolean ready() void reset() long skip(long charCount)
示例代碼:
關於CharArrayReader中API的具體用法,參考示例代碼(CharArrayReaderTest.java):
import java.io.CharArrayReader; import java.io.CharArrayWriter; import java.io.IOException; public class CharArrayReaderTest { private static final int LEN = 5; // 對應英文字母“abcdefghijklmnopqrstuvwxyz” private static final char[] ArrayLetters = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; public static void main(String[] args) { tesCharArrayReader() ; } /** * CharArrayReader的API測試函數 */ private static void tesCharArrayReader() { try { // 創立CharArrayReader字符流,內容是ArrayLetters數組 CharArrayReader car = new CharArrayReader(ArrayLetters); // 從字符數組流中讀取5個字符 for (int i=0; i<LEN; i++) { // 若能持續讀取下一個字符,則讀取下一個字符 if (car.ready() == true) { // 讀取“字符流的下一個字符” char tmp = (char)car.read(); System.out.printf("%d : %c\n", i, tmp); } } // 若“該字符流”不支撐標志功效,則直接加入 if (!car.markSupported()) { System.out.println("make not supported!"); return ; } // 標志“字符流中下一個被讀取的地位”。即--標志“f”,由於由於後面曾經讀取了5個字符,所以下一個被讀取的地位是第6個字符” // (01), CharArrayReader類的mark(0)函數中的“參數0”是沒有現實意義的。 // (02), mark()與reset()是配套的,reset()會將“字符流中下一個被讀取的地位”重置為“mark()中所保留的地位” car.mark(0); // 跳過5個字符。跳過5個字符後,字符流中下一個被讀取的值應當是“k”。 car.skip(5); // 從字符流中讀取5個數據。即讀取“klmno” char[] buf = new char[LEN]; car.read(buf, 0, LEN); System.out.printf("buf=%s\n", String.valueOf(buf)); // 重置“字符流”:即,將“字符流中下一個被讀取的地位”重置到“mark()所標志的地位”,即f。 car.reset(); // 從“重置後的字符流”中讀取5個字符到buf中。即讀取“fghij” car.read(buf, 0, LEN); System.out.printf("buf=%s\n", String.valueOf(buf)); } catch (IOException e) { e.printStackTrace(); } } }
運轉成果:
0 : a 1 : b 2 : c 3 : d 4 : e buf=klmno buf=fghij
CharArrayWriter
CharArrayReader 用於寫入數據符,它繼續於Writer。操作的數據是以字符為單元!
CharArrayWriter 函數列表
CharArrayWriter() CharArrayWriter(int initialSize) CharArrayWriter append(CharSequence csq, int start, int end) CharArrayWriter append(char c) CharArrayWriter append(CharSequence csq) void close() void flush() void reset() int size() char[] toCharArray() String toString() void write(char[] buffer, int offset, int len) void write(int oneChar) void write(String str, int offset, int count) void writeTo(Writer out)
示例代碼:
關於CharArrayWriter中API的具體用法,參考示例代碼(CharArrayWriterTest.java):
import java.io.CharArrayReader; import java.io.CharArrayWriter; import java.io.IOException; public class CharArrayWriterTest { private static final int LEN = 5; // 對應英文字母“abcdefghijklmnopqrstuvwxyz” private static final char[] ArrayLetters = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; public static void main(String[] args) { tesCharArrayWriter() ; } /** * CharArrayWriter的API測試函數 */ private static void tesCharArrayWriter() { try { // 創立CharArrayWriter字符流 CharArrayWriter caw = new CharArrayWriter(); // 寫入“A”個字符 caw.write('A'); // 寫入字符串“BC”個字符 caw.write("BC"); //System.out.printf("caw=%s\n", caw); // 將ArrayLetters數組中從“3”開端的後5個字符(defgh)寫入到caw中。 caw.write(ArrayLetters, 3, 5); //System.out.printf("caw=%s\n", caw); // (01) 寫入字符0 // (02) 然後接著寫入“123456789” // (03) 再接著寫入ArrayLetters中第8-12個字符(ijkl) caw.append('0').append("123456789").append(String.valueOf(ArrayLetters), 8, 12); System.out.printf("caw=%s\n", caw); // 盤算長度 int size = caw.size(); System.out.printf("size=%s\n", size); // 轉換成byte[]數組 char[] buf = caw.toCharArray(); System.out.printf("buf=%s\n", String.valueOf(buf)); // 將caw寫入到另外一個輸入流中 CharArrayWriter caw2 = new CharArrayWriter(); caw.writeTo(caw2); System.out.printf("caw2=%s\n", caw2); } catch (IOException e) { e.printStackTrace(); } } }
運轉成果:
caw=ABCdefgh0123456789ijkl size=22 buf=ABCdefgh0123456789ijkl caw2=ABCdefgh0123456789ijkl