用IO流实现文件复制功能

来源:互联网 发布:三巨网络面试如何 编辑:程序博客网 时间:2024/05/29 19:53
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);}
原创粉丝点击