A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 奋斗的小菇凉 中级黑马   /  2015-8-16 20:52  /  202 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

复制文件的几种代码体现

1 个回复

倒序浏览
1 import java.io.File;
2  import java.io.FileInputStream;
3 import java.io.FileOutputStream;
4 import java.io.InputStream;
5
6
7 public class FileCopy {
8     public static void main(String[] args)
9     {
10           try
11             {
12                 int bytesum = 0;
13                 int byteread = 0;
14                 File oldfile = new File( "c:/test.txt");
15                 if ( oldfile.exists() )
16                 { //文件存在时
17                     InputStream inStream = new FileInputStream( "c:/test.txt" ); //读入原文件
18                     FileOutputStream fs = new FileOutputStream( "d:/test.txt" );
19                     byte[] buffer = new byte[ 1444 ];
20                     int length;
21                     while ( ( byteread = inStream.read( buffer ) ) != -1 )
22                     {
23                         bytesum += byteread; //字节数 文件大小
24                         System.out.println( bytesum );
25                         fs.write( buffer, 0, byteread );
26                     }
27                     inStream.close();
28                 }
29             }
30             catch ( Exception e )
31             {
32                 System.out.println( "复制单个文件操作出错" );
33                 e.printStackTrace();
34
35             }
36     }
37 }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马