java复制单个文件代码~

  
    
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 }
38

 

你可能感兴趣的:(java)