Java中的PushbackReader類的read(char [],int,int)方法用於將指定長度的字符以指定的偏移量讀取到數組中。此方法將流阻塞到:
- 它已從流中獲取了一些輸入。
- 發生了一些IOException
- 閱讀時已到達流的末尾。
用法:
public int read(char[] charArray, int offset, int length)
參數:此方法接受三個強製參數:
- charArray這是要寫入流中的字符數組。
- offset這是要在數組中寫入字符的偏移量索引。
- length這是要寫入數組的字符數。
返回值:此方法返回一個整數值,該整數值是從流中讀取的字符數。如果未讀取任何字符,則返回-1。
異常:此方法引發以下異常:
- IOException:輸入輸出時發生錯誤。
- IndexOutOfBoundsException:如果偏移值不在字符數組範圍內
以下方法說明了read(char [],int,int)方法的用法方式:
程序1:
// Java program to demonstrate
// PushbackReader read(char, int, int) method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
// Initializing a StringReader
// and PushbackReader
String s = "GeeksForGeeks";
StringReader stringReader
= new StringReader(s);
PushbackReader pushbackReader
= new PushbackReader(stringReader);
// Get the character array
// to be read from the stream
char[] charArray = new char[5];
// Get the offset index
int offset = 0;
// Get the length
int length = 5;
// Read the charArray
// to this reader using read() method
// This will put the str in the stream
// till it is read by the reader
pushbackReader
.read(charArray, offset, length);
// Print the read charArray
System.out.println(
Arrays.toString(charArray));
// Close the stream
pushbackReader.close();
System.out.println("Stream Closed.");
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
[G, e, e, k, s] Stream Closed.
程序2:
// Java program to demonstrate
// PushbackReader read(char, int, int) method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
// Initializing a StringReader
// and PushbackReader
String s = "GFG";
StringReader stringReader
= new StringReader(s);
PushbackReader pushbackReader
= new PushbackReader(stringReader);
// Get the character array
// to be read from the stream
char[] charArray = new char[5];
// Get the offset index
int offset = 0;
// Get the length
int length = 2;
// Read the charArray
// to this reader using read() method
// This will put the str in the stream
// till it is read by the reader
pushbackReader
.read(charArray, offset, length);
// Print the read charArray
System.out.println(
Arrays.toString(charArray));
// Close the stream
pushbackReader.close();
System.out.println("Stream Closed.");
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
[G, F,,, ] Stream Closed.
參考: https://docs.oracle.com/javase/9/docs/api/java/io/PushbackReader.html#read-char:A-int-int-
相關用法
- Java PushbackReader close()用法及代碼示例
- Java PushbackReader markSupported()用法及代碼示例
- Java PushbackReader mark(int)用法及代碼示例
- Java PushbackReader read()用法及代碼示例
- Java PushbackReader ready()用法及代碼示例
- Java PushbackReader reset()用法及代碼示例
- Java PushbackReader skip(long)用法及代碼示例
- Java Java.io.PushbackReader用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
注:本文由純淨天空篩選整理自srinam大神的英文原創作品 PushbackReader read(char, int, int) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。