1、实例化
DataInputStream(InputStream in) | 参数是一个字节输入流 |
---|---|
DataOutputStream(OutputStream out) | 参数是一个字节输出流 |
演示
DataInputStream dis = new DataInputStream(new FileInputStream("D:/test/girl.jpg"));
DataOutputStream dos = new DataOutputStream(new FileOutputStream("D:/test/girl2.jpg"));
2、举例
复制图片
代码语言:javascript复制 try(
DataInputStream dis = new DataInputStream(new FileInputStream("D:/test/girl.jpg"));
DataOutputStream dos = new DataOutputStream(new FileOutputStream("D:/test/girl2.jpg"));
){
//字符缓冲区
byte[] bytes = new byte[10];
//读取长度
int hasread = 0;
while((hasread = dis.read(bytes))>0){
dos.write(bytes,0,hasread);
}
//刷新缓冲区
dos.flush();
System.out.println("图片复制成功");
}catch (Exception e){
e.printStackTrace();
}