用DataInputStream完成文本,视频,音频的复制,其他流的代码类似
1,DataInputStream是高级流,所以要以低级流为基础,在构造方法中写入低级流的参数
**注意:在复制音频,视频的时候,不能用字符流,因为会导致复制前后大小不一致
package com.hyxy.xiancheng;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyDataInputStream {
public static void main(String[] args) {
//copyText();
copyMusic();
}
//复制文本
public static void copyText(){
DataInputStream ds = null;
DataOutputStream os = null;
try {
ds = new DataInputStream(new FileInputStream("bw.txt"));
os = new DataOutputStream(new FileOutputStream("os.txt"));
byte b [] = new byte[1024];
int len = 0;
while((len = ds.read(b))!=-1){
os.write(b,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
ds.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//复制音频
public static void copyMusic(){
DataInputStream ds = null;
DataOutputStream os = null;
try {
ds = new DataInputStream(new FileInputStream("D:\\KuGou/Veela、Feint - Vagrant.mp3"));
os = new DataOutputStream(new FileOutputStream("os1.mp3"));
byte b [] = new byte[1024];
int len = 0;
while((len = ds.read(b))!=-1){
os.write(b,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
ds.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}