ObjectInputStream 类的 readChar() 方法用于读取 16 位(2 字节)字符。
用法
public char readChar() throws IOException
参数
没有传递参数。
返回
读取的 16 位字符。
抛出
EOFException - 如果到达文件末尾。
IOException - 如果发生其他 I/O 错误。
例子1
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ObjectInputStreamreadCharsExample1 {
public static void main(String[] args) throws IOException {
byte[] buf = {'j', 'a', 'v', 'a', 'T', 'p', 'o', 'i', 'n', 't'};
try {
// create file output stream
FileOutputStream fileoutstream = new FileOutputStream("file1.txt");
// create data output stream
DataOutputStream dataoutstream = new DataOutputStream(fileoutstream);
// for each byte in the buffer
for (byte b:buf) {
// write character to the dos
dataoutstream.writeChar(b);
}
// force bytes to the underlying stream
dataoutstream.flush();
// create file input stream
InputStream inpstream = new FileInputStream("file1.txt");
// create new data input stream
DataInputStream datainpstream = new DataInputStream(inpstream);
// read till end of the stream
while(datainpstream.available()>0) {
// read character
char c = datainpstream.readChar();
// print
System.out.print(" "+c);
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
输出:
j a v a T p o i n t
例子2
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ObjectInputStreamreadCharsExample2 {
public static void main(String[] args) throws IOException {
byte[] carray = {'j', 'a', 'v', 'a', 'T', 'p', 'o', 'i', 'n', 't'};
try {
// create file output stream
FileOutputStream fileoutstream = new FileOutputStream("file1.txt");
// create data output stream
DataOutputStream dataoutstream = new DataOutputStream(fileoutstream);
// for each byte in the buffer
for (byte b:carray) {
// write character to the dos
dataoutstream.writeChar(b);
}
// force bytes to the underlying stream
dataoutstream.flush();
// create file input stream
InputStream inpstream = new FileInputStream("file1.txt");
// create new data input stream
DataInputStream datainpstream = new DataInputStream(inpstream);
// read till end of the stream
for(int i=0;i<=carray.length;i++){
// read character
char c = datainpstream.readChar();
// print
System.out.print(" "+c);
}
} catch(Exception e) {
}
}
}
输出:
j a v a T p o i n t
相关用法
- Java ObjectInputStream readChar()用法及代码示例
- Java ObjectInputStream readDouble()用法及代码示例
- Java ObjectInputStream readUnsignedByte()用法及代码示例
- Java ObjectInputStream readFields()用法及代码示例
- Java ObjectInputStream readFully()用法及代码示例
- Java ObjectInputStream readInt()用法及代码示例
- Java ObjectInputStream read()用法及代码示例
- Java ObjectInputStream readUTF()用法及代码示例
- Java ObjectInputStream readField()用法及代码示例
- Java ObjectInputStream readShort()用法及代码示例
- Java ObjectInputStream readUnsingedByte()用法及代码示例
- Java ObjectInputStream readByte()用法及代码示例
- Java ObjectInputStream readLong()用法及代码示例
- Java ObjectInputStream readStreamHeader()用法及代码示例
- Java ObjectInputStream readUnsignedShort()用法及代码示例
- Java ObjectInputStream readObjectOverride()用法及代码示例
- Java ObjectInputStream readUnshared()用法及代码示例
- Java ObjectInputStream readObject()用法及代码示例
- Java ObjectInputStream readLine()用法及代码示例
- Java ObjectInputStream readBoolean()用法及代码示例
注:本文由纯净天空筛选整理自 Java ObjectInputStream readChar() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。