前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java实现文件复制常见方式

Java实现文件复制常见方式

作者头像
BUG弄潮儿
发布2020-06-15 17:44:53
6360
发布2020-06-15 17:44:53
举报
文章被收录于专栏:JAVA乐园JAVA乐园

阅读文本大概需要3分钟。

0x01:FileInputStream/FileOutputStream字节流进行文件的复制

代码语言:javascript
复制
private static void streamCopyFile(File srcFile, File desFile) {
       try{
          // 使用字节流进行文件复制
          FileInputStream fi = new FileInputStream(srcFile);
          FileOutputStream fo = new FileOutputStream(desFile);
          Integer by = 0;
          //一次读取一个字节
          while((by = fi.read()) != -1) {
              fo.write(by);
          }
          fi.close();
          fo.close();
       }catch(Exception e){
          e.printStackTrace();
       }

 }

0x02:BufferedInputStream/BufferedOutputStream高效字节流进行复制文件

代码语言:javascript
复制
private static void bufferedStreamCopyFile(File srcFile, File desFile){
    try{
         // 使用缓冲字节流进行文件复制
         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desFile));
         byte[] b = new byte[1024];
         Integer len = 0;
         //一次读取1024字节的数据
         while((len = bis.read(b)) != -1) {
             bos.write(b, 0, len);
         }
         bis.close();
         bos.close();
     }catch(Exception e){
          e.printStackTrace();
       }
}

0x03: FileReader/FileWriter字符流进行文件复制文件

代码语言:javascript
复制
private static void readerWriterCopyFile(File srcFile, File desFile){
    try{
         // 使用字符流进行文件复制,注意:字符流只能复制只含有汉字的文件
          FileReader fr = new FileReader(srcFile);
          FileWriter fw = new FileWriter(desFile);

          Integer by = 0;
          while((by = fr.read()) != -1) {
              fw.write(by);
          }

         fr.close();
         fw.close();
     }catch(Exception e){
          e.printStackTrace();
     }
}

0x04: BufferedReader/BufferedWriter高效字符流进行文件复制

代码语言:javascript
复制
private static void bufferedReaderWriterCopyFile(File srcFile, File desFile){
    try{
         // 使用带缓冲区的高效字符流进行文件复制
          BufferedReader br = new BufferedReader(new FileReader(srcFile));
          BufferedWriter bw = new BufferedWriter(new FileWriter(desFile));

          char[] c = new char[1024];
          Integer len = 0;
          while((len = br.read(c)) != -1) {
              bw.write(c, 0, len);
         }

         //方式二
         /*
         String s = null;
         while((s = br.readLine()) != null) {
             bw.write(s);
             bw.newLine();
         }
        */

         br.close();
         bw.close();
     }catch(Exception e){
          e.printStackTrace();
     }
}

0x05: NIO实现文件拷贝(用transferTo的实现 或者transferFrom的实现)

代码语言:javascript
复制
public static void  NIOCopyFile(String  source,String target) {
   try{
       //1.采用RandomAccessFile双向通道完成,rw表示具有读写权限
       RandomAccessFile fromFile = new RandomAccessFile(source,"rw");
       FileChannel fromChannel = fromFile.getChannel();

       RandomAccessFile toFile = new RandomAccessFile(target,"rw");
       FileChannel toChannel = toFile.getChannel();

       long count =  fromChannel.size();
       while (count > 0) {
           long transferred = fromChannel.transferTo(fromChannel.position(), count, toChannel);
           count -= transferred;
        }
       if(fromFile!=null) {
           fromFile.close();
       }
       if(fromChannel!=null) {
           fromChannel.close();
       }
   }catch(Exception e){
        e.printStackTrace();
   }
}

0x06: java.nio.file.Files.copy()实现文件拷贝,其中第三个参数决定是否覆盖

代码语言:javascript
复制
public static void copyFile(String source,String target){
    Path sourcePath = Paths.get(source);
    Path destinationPath = Paths.get(target);
    try {
        Files.copy(sourcePath, destinationPath,
                StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 BUG弄潮儿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档