为了提升文件的复制速度,我们将字符流改进为二进制流进行复制。代码如下
package fuzhiwenjian;
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 copy {
public static void main(String[] args) {
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream("b.mp3",true);
FileInputStream inputStream = new FileInputStream("a.mp3");
DataInputStream dataInput = new DataInputStream(inputStream);
DataOutputStream dataOutput = new DataOutputStream(outputStream);
byte []wxj = new byte[1024];
int length = dataInput.read(wxj);
while(length!=-1){
dataOutput.write(wxj,0,length);
length = dataInput.read(wxj);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
通过本次作业,我意识到了因该在未来的实践中,在达到目标的同时,尽可能的提高效率,使其亦达到最大化。