首页  >  文章  >  Java  >  Java实现文件拷贝:使用字符流进行操作

Java实现文件拷贝:使用字符流进行操作

WBOY
WBOY转载
2023-05-06 17:04:081136浏览

通过字符流实现文件拷贝

使用字符流只能拷贝文本文件

  /**
   * 通过字符流实现文件的拷贝
   *
   * @param sourcePath 源文件路径
   * @param targetPath 目标文件路径
   */
  public static void copyFileByReaderAndWriter(String sourcePath, String targetPath) {
    //源文件路径
    File source = new File(sourcePath);
    //目标文件路径
    File target = new File(targetPath);

    //如果源文件不存在则不能拷贝
    if (!source.exists()) {
      return;
    }
    //如果目标文件目录不存在则创建
    if (!target.getParentFile().exists()) {
      target.getParentFile().mkdirs();
    }

    FileReader in = null;
    FileWriter out = null;
    try {
      //字符输入流和字符输出流
      in = new FileReader(source);
      out = new FileWriter(target);

      char[] c = new char[1024];
      int temp = 0;
      //每次读取1024个字符
      while ((temp = in.read(c)) != -1) {
        //输出到文件
        out.write(c, 0, temp);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      //关闭流
      try {
        if (in != null) {
          in.close();
        }
        if (out != null) {
          out.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

以上是Java实现文件拷贝:使用字符流进行操作的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:yisu.com。如有侵权,请联系admin@php.cn删除