您的位置:首页 > 其它

用IO流实现文件复制功能

2012-12-15 10:26 218 查看
public static void main(String[] args) throws Exception {
System.out.println("请输入读取文件(如文本,图片等)的路径:(如:D:\\Img.jpg)");
Scanner scannerIn = new Scanner(System.in);
String fileIn =scannerIn.nextLine();

System.out.println("请输入保存文件的路径。(如:E:\\abc.jpg)");
Scanner scannerout = new Scanner(System.in);
String fileOut =scannerout.nextLine();

FileInputStream fis = new FileInputStream(new File(fileIn));
FileOutputStream fos = new FileOutputStream(new File(fileOut));

byte[] bytes = new byte[1024*1024*4];//最大4M的缓冲区

while (fis.read(bytes)!=-1) {
fos.write(bytes);
}
fis.close();
fos.close();
System.out.println("文件保存成功,请在硬盘上查看"+fileOut);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息