Java基础知识之文件操作(二)——IO流复制文件
2017-03-28 10:26
656 查看
1.复制文件
核心代码
具体代码
2.复制文件夹
遍历子文件夹,是文件则调用文件复制的方法,是文件夹则递归调用自己继续遍历并复制
3.按行读取文本文件内容,并以指定编码写入另一文本文件
读取数据
写入数据
若需要追加内容则使用 FileOutputStream(Filefile,true)
核心代码
inputStream = new FileInputStream(src); outputStream = new FileOutputStream(direct); byte[] buffer = new byte[1024]; // 读取的字节数 int length = 0; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); }
具体代码
// srcName为复制文件的路径名, directName为目标文件的路径名, public static boolean copyFile(String srcName, String directName) { File src = new File(srcName); // 如果复制文件不存在 if (!src.exists()) { System.out.println("复制文件不存在"); return false; } else if (!src.isFile()) { // 如果src指定路径不是文件 也无法复制 System.out.println("复制文件不是文件"); return false; } File direct = new File(directName); if (direct.getParentFile() == null) { // 如果目标文件没有父路径 则在复制在复制文件的路径下 // 组拼目标文件的路径 String dir = src.getParent() + File.separator + directName; direct = new File(dir); } else { // 如果目标文件有父目录,则创建目录文件夹 if (!direct.getParentFile().exists()) { // 如果创建目录文件夹失败 if (!direct.getParentFile().mkdirs()) { System.out.println("目标文件路径错误"); return false; } } } // 判断目标文件是否存在 if (direct.exists()) { System.out.println("目标文件已存在"); return false; } // 输入流 InputStream inputStream = null; // 输出流 OutputStream outputStream = null; try { inputStream = new FileInputStream(src); outputStream = new FileOutputStream(direct); byte[] buffer = new byte[1024]; // 读取的字节数 int length = 0; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } // 复制成功 返回true return true; } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } finally { // 关闭流 try { if (outputStream != null) { outputStream.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }
2.复制文件夹
遍历子文件夹,是文件则调用文件复制的方法,是文件夹则递归调用自己继续遍历并复制
// srcName为复制文件的路径名, directName为目标文件的路径名 public static boolean copyDirectory(String srcName, String directName) { File src = new File(srcName); // 如果文件夹不存在 if (!src.exists()) { System.out.println("文件夹不存在"); return false; } else if (!src.isDirectory()) { // 如果不是文件夹 System.out.println("复制文件不是文件夹"); return false; } File direct = new File(directName); // 如果目标目录名不是以文件分隔符结尾,则加上文件分隔符 if (!directName.endsWith(File.separator)) { direct = new File(directName + File.separator); } if (direct.getParentFile() == null) { // 如果目标文件没有父路径 则在复制在复制文件的路径下 // 组拼目标文件的路径 String dir = src.getParent() + File.separator + direct.getName(); direct = new File(dir); } else { // 如果目标文件有父目录,则创建目录文件夹 if (!direct.getParentFile().exists()) { // 如果创建目录文件夹失败 if (!direct.getParentFile().mkdirs()) { System.out.println("目标文件路径错误"); return false; } } } // 如果目标文件夹存在 if (direct.exists()) { // 则返回 false System.out.println("目标文件已存在"); return false; } else { // 不存在则创建文件夹 direct.mkdirs(); } // 判断是否复制成功 boolean flag = true; // 复制文件 列出文件夹下所有文件 File[] files = src.listFiles(); // 遍历所有文件 for (File file : files) { // 如果是文件 if (file.isFile()) { // 调用复制文件的方法 // 因为前面改变过directName,所以在此处使用direct.getAbsolutePath() 拼接路径 flag = copyFile(file.getPath(), direct.getAbsolutePath() + File.separator + file.getName()); if (!flag) { break; } } else if (file.isDirectory()) { // 递归调用,继续复制文件夹 flag = copyDirectory(file.getPath(), direct.getAbsolutePath() + File.separator + file.getName()); if (!flag) { break; } } } if (!flag) { System.out.println("复制失败"); return false; } else { return true; } }
3.按行读取文本文件内容,并以指定编码写入另一文本文件
读取数据
//path 被读取文本的路径, encoding编码 public static String read(String path, String encoding) throws IOException { StringBuffer stringBuffer = new StringBuffer(); File file = new File(path); BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(file), encoding)); String content = null; while((content = reader.readLine())!= null) { //"\r\n" 换行 stringBuffer.append(content + "\r\n"); } reader.close(); return stringBuffer.toString(); }
写入数据
若需要追加内容则使用 FileOutputStream(Filefile,true)
//path 写入文件的路径,content写入内容,encoding编码 public static void write(String path, String content, String encoding) throws IOException { File file = new File(path); // 有则删除 file.delete(); // 创建新文件 file.createNewFile(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file), encoding)); // 写数据 writer.write(content); writer.close(); }
相关文章推荐
- 黑马程序员--IO流和文件操作--java学习日记8(基础知识)
- 【Java基础知识】IO流--字节流读写数据以及复制文件的几种方式
- Java基础之IO流,通过字节流缓冲区进行媒体文件的复制操作
- Java基础知识强化之IO流笔记49:IO流练习之 复制指定目录下指定后缀名的文件并修改名称的案例
- Java基础之IO流,通过字节流对媒体文件进行复制操作
- Java基础知识强化之IO流笔记14:递归之输出指定目录下所有java文件绝对路径的案例
- 黑马程序员_Java基础_IO流_字节流,字节流操作文件,缓冲区字节流,流的使用规律,异常记录原理
- Java基础之IO流,合并流对象SequenceInputStream对文件的切割与合并操作
- Java基础之IO流,以字节流的方式操作读写文件FileOutputStream和FileInputStream的使用
- Java基础知识强化之IO流笔记24:FileInputStream / FileOutputStream 复制文本文件案例2
- Java基础知识强化之IO流笔记36:InputStreamReader/OutputStreamWriter 复制文本文件案例
- Java基础知识强化之IO流笔记37:FileReader/FileWriter(转换流的子类)复制文本文件案例
- Java基础知识强化之IO流笔记25:FileInputStream / FileOutputStream 复制图片案例
- Java基础——IO流和文件操作(下)
- IO流-1.【IO流基础知识】【字符流—Writer/Reader】【文本文件复制案例】
- Java基础之IO流,文件的复制原理与示例
- JavaSE IO流 File文件操作基础知识
- java中最简单的文件复制,读写(IO流操作)
- 03_java基础_IO流和文件操作
- Java基础——IO流与文件操作(上)