import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@SuppressWarnings("all")
public class ReadAndWriteBinaryImageDemo {
public static void main(String[] args){
FileInputStream fis = null;//创建文件读入流
DataInputStream dis = null;//创建二进制读入流
FileOutputStream fos = null;//创建文件输出流
DataOutputStream dos = null;//创建二进制输出流
try {
//创建读入流对象
fis = new FileInputStream("D:/2.jpg");
//将对象转化为二进制
dis = new DataInputStream(fis);
//创建输出流对象
fos = new FileOutputStream("D:/test/sa.bmp");
//将输出流对象转化为二进制
dos = new DataOutputStream(fos);
int temp;
try {
temp = fis.read();
while(temp != -1) {
dos.write(temp);
temp = fis.read();
}
System.out.println("图片写入成功!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {//关闭流,释放资源
try {
if(fis != null)
fis.close();
if(dis != null)
dis.close();
if(fos != null)
fos.close();
if(dos != null)
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}