I/O流对图片的读和写

public static void main(String[]args) throws IOException{

String path = "C:/Users/86159/Desktop/timg.jpg"; //图片地址
File file = new File(path); //将图片转换成file类型的文件
FileInputStream fis = new FileInputStream(file); //创建输入流
byte[] byt = new byte[fis.available()]; //字节数据 available() 返回的实际可读字节数,也就是总大小
StringBuilder str = new StringBuilder();// 不建议用String
fis.read(byt); // 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数。
for (byte bs : byt) {
// 将字节数组byte[] 转换为二进制,在以字符串的形式显示出来
//Integer.toBinaryString(bs)返回无符号整数的二进制的字符串表示形式
str.append(Integer.toBinaryString(bs));
}
str.toString();

System.out.println("输出二进制字符串+==="+str);

// 把字节数组的图片写到另一个地方
File apple = new File("D:/apple.jpg");
FileOutputStream fos = new FileOutputStream(apple);
fos.write(byt);
fos.flush();
fos.close();
}
posted @ 2021-01-25 16:12  java程序猴  阅读(118)  评论(0编辑  收藏  举报