gpt4 book ai didi

java文件复制代码片断(java实现文件拷贝)

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章java文件复制代码片断(java实现文件拷贝)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

1、要完成这个程序需要了解的知识点:

1、编写简单的Java程序,比如hello world ---废话了。。。。哈哈 。

2、了解java的文件操作 。

3、了解java的buffer操作 。

4、对文件操作的一些异常处理点:1、源文件不能读取到的情况。 2、目的文件创建失败的情况 3、文件锁问题 4、字符乱码问题。。。可能不全啊 。

这些是需要用到的包 。

import java.io.BufferedInputStream,

import java.io.BufferedOutputStream,

import java.io.FileInputStream,

import java.io.FileOutputStream,

import java.io.IOException; IO操作时需要做异常处理 。

个人感觉这个效率高的方式,安装计算机来讲,效率高的操作应该是对内存的操作是比较高的了,直接对IO的操作应该是相对低的。。所以这里选的是就是读到内存在统一写IO,代码如下:

  1. package com.itheima; 
  2.   
  3. import java.io.BufferedInputStream; 
  4. import java.io.BufferedOutputStream; 
  5. import java.io.FileInputStream; 
  6. import java.io.FileOutputStream; 
  7. import java.io.IOException; 
  8.   
  9. /** 
  10.  * 5、 编写程序拷贝一个文件, 尽量使用效率高的方式. 
  11.  *  
  12.  * @author 2811671413@qq.com 
  13.  *  
  14.  *     1、源文件不能读取到的情况。 2、目的文件创建失败的情况 3、文件锁问题 4、字符乱码问题 
  15.  */ 
  16.   
  17. public class Test5 { 
  18.   
  19.     public static void main(String[] args) throws IOException { 
  20.         String src_file = "D:/java/java.doc"
  21.         String des_file = "D:/java/java_copy.doc"
  22.           
  23.         copyFile(src_file, des_file); 
  24.           
  25.         System.out.println("OK!"); 
  26.     } 
  27.   
  28.     public static void copyFile(String src, String des) throws IOException { 
  29.         BufferedInputStream inBuff = null
  30.         BufferedOutputStream outBuff = null
  31.           
  32.         try { 
  33.             // 新建文件输入流并对它进行缓冲 
  34.             inBuff = new BufferedInputStream(new FileInputStream(src)); 
  35.   
  36.             // 新建文件输出流并对它进行缓冲 
  37.             outBuff = new BufferedOutputStream(new FileOutputStream(des)); 
  38.   
  39.             // 缓冲数组 
  40.             byte[] b = new byte[1024 * 5]; 
  41.             int len; 
  42.             while ((len = inBuff.read(b)) != -1) { 
  43.                 outBuff.write(b, 0, len); 
  44.             } 
  45.             // 刷新此缓冲的输出流 
  46.             outBuff.flush(); 
  47.         } finally { 
  48.             // 关闭流 
  49.             if (inBuff != null
  50.                 inBuff.close(); 
  51.             if (outBuff != null
  52.                 outBuff.close(); 
  53.         } 
  54.   
  55.     } 

其它网友的补充 。

  1. try { 
  2.       File inputFile = new File(args[0]); 
  3.       if (!inputFile.exists()) { 
  4.         System.out.println("源文件不存在,程序终止"); 
  5.         System.exit(1); 
  6.       } 
  7.       File outputFile = new File(args[1]); 
  8.       InputStream in = new FileInputStream(inputFile); 
  9.       OutputStream out = new FileOutputStream(outputFile); 
  10.       byte date[] = new byte[1024]; 
  11.       int temp = 0; 
  12.       while ((temp = in.read(date)) != -1) { 
  13.         out.write(date); 
  14.       } 
  15.       in.close(); 
  16.       out.close(); 
  17.     } catch (FileNotFoundException e) { 
  18.       // TODO Auto-generated catch block 
  19.       e.printStackTrace(); 
  20.     } catch (IOException e) { 
  21.       // TODO Auto-generated catch block 
  22.       e.printStackTrace(); 
  23.     } 

最后此篇关于java文件复制代码片断(java实现文件拷贝)的文章就讲到这里了,如果你想了解更多关于java文件复制代码片断(java实现文件拷贝)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com