java实现文件拷贝

摘要:
=null){28input.close();29}30if(输出!
 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 }

免责声明:文章转载自《java实现文件拷贝》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ubuntu server安装的一些坑常用的正则验证下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

微信小程序 View:flex 布局

微信小程序View支持两种布局方式:Block和Flex所有View默认都是block要使用flex布局的话需要显式的声明:display:flex;下面就来介绍下微信小程序的Flex布局先做一个简单的demo123加上背景色能看的更清楚些.main{width:100%;background-color:antiquewhite;}.item{height...

搭建Squid3 密码账号IP代理

默认端口是端口3128,它已被滥用。注意修改端口,并应用Squid密码帐户方法保护流量...

如何设置Navicat的显示字体与字体大小?

方法/步骤打开Navicat点击菜单,再选择在界面,点击下的设置网格字体和大小设置编辑器字体和大小设置命令列界面字体和大小设置ER图表字体和大小,最后点击END...

CUPS

杯子:一个。工具1.hal设备管理器2.系统配置打印机3.Web管理器/etc/cups/ccups。conf/etc/cups/printer conf II。打印机本地安装和客户端安装1.在本地安装Linux打印机时,应选择postscript和pcl打印机。如果没有,则应将打印机设置为原始打印模式/etc/cups/printers。有限公司...

electron用默认浏览器打开链接的3种实现方式

在使用Electron开发桌面程序的过程中,我们可能经常需要使Electron程序中包含的链接在单击后直接调用系统的默认浏览器打开。仔细阅读文档后,我们都知道它的核心原理是调用系统的默认浏览器,通过Electron shell模块中的openExternal方法打开链接。然而,它的实现有不同的方法,彻底接管和选择性接管。介绍第3章中的有效方法。以上三种方法都...

CefSharp 浏览器核心,爬虫

CefSharp是什么Aframeworkforembeddingweb-browsing-likecapabilitiestoastandard.NETapplication(WPForWindowsForms).Asetofhigh-levelcustomcontrolstomakeitreasonablyeasytointegratethesecapa...