zoukankan      html  css  js  c++  java
  • java实现文件拷贝

     1 package com.liu.test;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     7 import java.nio.channels.FileChannel;
     8  
     9 public class testFile {
    10  
    11     public static void main(String[] args) {
    12         fileCopy_channel();
    13     }
    14      
    15     public static void fileCopy_channel() {
    16         FileChannel input = null;
    17         FileChannel output = null;
    18  
    19         try {
    20             input = new FileInputStream("C:\Users\RD-10\Downloads\tiles.rar").getChannel();
    21             output = new FileOutputStream("D:\testFile\tiles.rar").getChannel();
    22             output.transferFrom(input, 0, input.size());
    23         } catch (Exception e) {
    24             e.printStackTrace();
    25         } finally {
    26             try {
    27                 if (input != null) {
    28                     input.close();
    29                 }
    30                 if (output != null) {
    31                     output.close();
    32                 }
    33             } catch (IOException e) {
    34                 e.printStackTrace();
    35             }
    36         }
    37     }
    38     /*
    39      * 实现文件的拷贝
    40      * 
    41      * @param srcPathStr 源文件的地址信息
    42      * 
    43      * @param desPathStr 目标文件的地址信息
    44      */
    45     private static void copyFile(String srcPathStr, String desPathStr) {
    46         // 1.获取源文件的名称
    47         String newFileName = srcPathStr.substring(srcPathStr.lastIndexOf("/") + 1); // 目标文件地址
    48         System.out.println("123"+newFileName);
    49         desPathStr = desPathStr + File.separator + newFileName; // 源文件地址
    50         System.out.println("1234"+desPathStr);
    51  
    52         try {
    53             // 2.创建输入输出流对象
    54             FileInputStream fis = new FileInputStream(srcPathStr);
    55             FileOutputStream fos = new FileOutputStream(desPathStr);
    56  
    57             // 创建搬运工具
    58             byte datas[] = new byte[1024 * 8];
    59             // 创建长度
    60             int len = 0;
    61             // 循环读取数据
    62             while ((len = fis.read(datas)) != -1) {
    63                 fos.write(datas, 0, len);
    64             }
    65             // 3.释放资源
    66             fis.close();
    67             fis.close();
    68         } catch (Exception e) {
    69             System.out.println("错误");
    70         }
    71     }
    72 }
  • 相关阅读:
    继承在WCF中的问题和解决办法
    bootstrap插件学习-bootstrap.dropdown.js
    C#山寨版本拨号客户端
    关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系
    [源码]Literacy 快速反射读写对象属性,字段
    Hadoop Streaming框架学习(一)
    AOP详解
    SESSION会话技术
    mongodb 备份、还原、导入、导出
    Qt 技巧: 解决未解析的SSL问题
  • 原文地址:https://www.cnblogs.com/lhq1996/p/13025855.html
Copyright © 2011-2022 走看看