java源文件分享地址:
链接:https://pan.baidu.com/s/16pcU-EkjAKhb1tYOQ5vJcg 提取码:lzsm 复制这段内容后打开百度网盘手机App,操作更方便哦
作业:通过IO流编写文件复制功能,编写代码如下:
首先创建文件复制类:
package com.com.filecopy; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileCopyDemo { public void fileCopy(String filePath1,String filePath2){ FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(filePath1); fos = new FileOutputStream(filePath2); int result = -1; String re = ""; while ((result = fis.read())!= -1){ re += (char)result; } System.out.println("读取文件信息成功!"); fos.write(re.getBytes(),0,re.getBytes().length); System.out.println("复制文件信息成功!"); fos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { fos.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
其次创建测试类:
package com.com.filecopy; public class Test { public static void main(String[] args) { FileCopyDemo fcd = new FileCopyDemo(); fcd.fileCopy("g:/新建文件夹/bdqn.txt","g:/新建文件夹/test.txt"); } }
输出结果为:
读取文件信息成功! 复制文件信息成功!