需求描述:
当前目录下有一个名为music.flac文件,若想复制该文件并重命名为music_copy.flac,该如何实现呢?(该文件大小为9336KB)
public class TestCopyFile {
/*
* File类中的RandomAccessFile可以对文件进行随机读写操作,使用该类也可以实现对文件的复制
*/
方法一:
@Test
public void testRAF() throws Exception {//使用该类下的read()方法, 从该文件读取一个字节的数据。
RandomAccessFile raf0 = new RandomAccessFile("."+File.separator
+"src"+File.separator
+"com"+File.separator
+"tarena"+File.separator
+"test"+File.separator
+"music.flac","r");
RandomAccessFile raf1 = new RandomAccessFile("music_copy.flac","rw");
int d = -1;
long l = System.currentTimeMillis();
while((d = raf0.read())!=-1) {
raf1.write(d);
}
long ll = System.currentTimeMillis();
System.out.println("复制完成!!!耗时:"+(ll-l)+"ms");//复制完成!!!耗时:205522ms
raf0.close();
raf1.close();
}
方法二:
@Test
public void testRAF0() throws Exception {
/*
* 使用该类下的read(byte[] b, int off, int len)方法, 从该文件读取最多 len个字节的数据到字节数组。
*/
RandomAccessFile raf0 = new RandomAccessFile("."+File.separator
+"src"+File.separator
+"com"+File.separator
+"tarena"+File.separator
+"test"+File.separator
+"music.flac","r");
RandomAccessFile raf1 = new RandomAccessFile("music_copy.flac","rw");
byte [] b = new byte[1024*10];
int len = -1;
long l = System.currentTimeMillis();
while((len = raf0.read(b))!=-1) {
raf1.write(b,0,len);
}
long ll = System.currentTimeMillis();
System.out.println("复制完成!!!耗时:"+(ll-l)+"ms");//复制完成!!!耗时:31ms
raf0.close();
raf1.close();
}
/*
*使用FileInputStream、FileOutputStream类实现对文件的复制
*/
方法三:
@Test
public void testFisAndFos() throws Exception {//使用FIS类中的read()方法, 从该输入流读取一个字节的数据。
FileInputStream fis = new FileInputStream("."+File.separa