1
我从文件和InputStreamReader中创建了一个InputStream对象。在java中同时使用InputStream和InputStreamReader
InputStream ips = new FileInputStream("c:\\data\\input.txt");
InputStreamReader isr = new InputStreamReader(ips);
我将基本读以字节形式的数据到缓冲器,但是当有来自时我应该在字符读我将“开关模式”并用的InputStreamReader
读取时间byte[] bbuffer = new byte[20];
char[] cbuffer = new char[20];
while(ips.read(buffer, 0, 20)!=-1){
doSomethingWithbBuffer(bbuffer);
// check every 20th byte and if it is 0 start reading as char
if(bbuffer[20] == 0){
while(isr.read(cbuffer, 0, 20)!=-1){
doSomethingWithcBuffer(cbuffer);
// check every 20th char if its # return to reading as byte
if(cbuffer[20] == '#'){
break;
}
}
}
}
是这是一种安全的方式来读取具有混合字符和字节数据的文件?
不要使用'DataInputStream'' readLine()',因为它自JDK 1.1开始被标记为弃用 – Kalsan