黑马程序员技术交流社区
标题:
copy文件的程序,大家可以试试哦
[打印本页]
作者:
编程小黑
时间:
2015-7-25 12:29
标题:
copy文件的程序,大家可以试试哦
//一共有四种方式,我用BufferedInputStream和BufferedOutStream写了一个较快的
//还有FileInputStream和FileOutputStream写了一个较慢的,仅供大家参考
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
class FileCopy
{
public static void main(String[] args) throws IOException
{
//单个字节流一次读一个写一个;FileInputStream;
FileCopy.copyFile3("D:\\abs141.avi","aaa.avi");
}
//方式四:
//方式三:缓冲字节流一次读一个数组;BufferedInputStream;BufferedOutStream;
public static void copyFile3(String star, String index)throws IOException
{
//原始写法
//FileInputStream fis = new FileInputStream("aaa.txt");
//BufferedInputStream bis = new BufferedInputStream(fis);
//用匿名对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(star));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(index));
Long time1 = System.currentTimeMillis();
byte[] bys = new byte[1024];
int len = 0;
while((len=bis.read(bys))!=-1)
{
bos.write(bys,0,len);
bos.flush();
}
Long time2 = System.currentTimeMillis();
System.out.println("总耗时"+(time2-time1)+"毫秒");
bos.close();
bis.close();
}
//方式二:字节流一次读一个数组;File
//方式一:字节流一次读一个写一个;FileInputStream,FileOutputStream;
public static void copyFile1(String star, String index)throws IOException
{
FileInputStream fis = new FileInputStream(star);
FileOutputStream fos = new FileOutputStream(index);
//计算复制的时间
Long time1 = System.currentTimeMillis();
//调用读入字节流对象方法读入数据
int by = 0;
while((by=fis.read())!=-1)
{
//System.out.print((char)by);
fos.write(by);
}
Long time2 = System.currentTimeMillis();
System.out.println("需要:"+(time2-time1)+"毫秒");
//释放资源
fos.close();
fis.close();
}
}
//方式三:缓冲字节流一次读一个数组;BufferedInputStream;BufferedOutStream;
public static void copyFile3(String star, String index)throws IOException
{
//原始写法
//FileInputStream fis = new FileInputStream("aaa.txt");
//BufferedInputStream bis = new BufferedInputStream(fis);
//用匿名对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(star));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(index));
Long time1 = System.currentTimeMillis();
byte[] bys = new byte[1024];
int len = 0;
while((len=bis.read(bys))!=-1)
{
bos.write(bys,0,len);
bos.flush();
}
Long time2 = System.currentTimeMillis();
System.out.println("总耗时"+(time2-time1)+"毫秒");
bos.close();
bis.close();
}
//方式二:字节流一次读一个数组;File
//方式一:字节流一次读一个写一个;FileInputStream,FileOutputStream;
//public static void copyFile1(String star, String index)throws IOException
//{
//FileInputStream fis = new FileInputStream(star);
//FileOutputStream fos = new FileOutputStream(index);
//计算复制的时间
//Long time1 = System.currentTimeMillis();
//调用读入字节流对象方法读入数据
//int by = 0;
//while((by=fis.read())!=-1)
//{
//System.out.print((char)by);
// fos.write(by);
// }
// Long time2 = System.currentTimeMillis();
// System.out.println("需要:"+(time2-time1)+"毫秒");
// //释放资源
// fos.close();
// fis.close();
//}
}
作者:
rxtm
时间:
2015-7-25 13:58
看着就好麻烦
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2