黑马程序员技术交流社区

标题: io流 [打印本页]

作者: 奋斗的小菇凉    时间: 2015-8-16 20:52
标题: io流
复制文件的几种代码体现
作者: 奋斗@的小青年    时间: 2015-8-16 21:10
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 }




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2