二进制文件传输(图片,音乐)

二进制关键字符

DataInputStream//写入方法

DataOutputStream//写出方法

使用方法:

1先用字节流读取,明确文件地址和文件名

2然后用该字节流对象作为参数构建二进制对象


实例:

public static void main(String[] args) {

FileInputStream fis = null;
FileOutputStream fos = null;
DataInputStream dis =null;
DataOutputStream dos = null;


try {
fis = new FileInputStream("d:/jinqian.jpg");
fos = new FileOutputStream("d:/jinqian5.jpg",true);
dis = new DataInputStream( fis);
dos = new DataOutputStream( fos);

int len ;
while (( len = dis.read())!=-1) {
dos.write( len );
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
dos.flush();
if (dos!=null) {
dos.close();
}
if (dis!=null) {
dis.close();
}
if (fos!=null) {
fos.close();
}
if (fis!=null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}


猜你喜欢

转载自blog.csdn.net/jinqianwang/article/details/80113119
今日推荐