字节输入流
public static void main(String[] args) throws IOException {
//创建字节输入流对象
FileInputStream fis = new FileInputStream("F:\\by.txt");
//读取单个字节,返回独到的字节
int by = fis.read();
System.out.println(by);
//数组循环读取,返回值是读取的字节数,与单个字节读取的返回值有差异
byte[] bys = new byte[1024];
int len;
while ((len = fis.read(bys)) != -1) {
System.out.println(new String (bys,0,len));
}
fis.close();
}
字节缓冲流
public static void main(String[] args) throws IOException {
//缓冲输出流
FileOutputStream fos = new FileOutputStream("F:\\缓冲流.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("abdhwqhj".getBytes());
//bos.flush();
bos.close();
//缓冲输入流
FileInputStream fis = new FileInputStream("F:\\缓冲流.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int by;
while((by=bis.read())!=-1){
System.out.print((char)by);
}
byte[] bys = new byte[1024 * 100];
int len;
while((len=bis.read(bys))!=-1){
System.out.print(new String(bys,0,len));
}
}