失乐园

技术之路

BlogJava 联系 聚合 管理
  19 Posts :: 44 Stories :: 40 Comments :: 0 Trackbacks
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;

public class FileDev02 
{    
    
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");

    
public static void copyUtil(String from,String to){
        
try
        {
            File source 
= new File(from);
            File destiny 
= new File(to);
            FileInputStream fis 
= new FileInputStream(source);
            BufferedInputStream bis 
= new BufferedInputStream(fis);

            
if (!destiny.exists())
            {
                destiny.createNewFile();
            }
            FileOutputStream fos 
= new FileOutputStream(to);
            BufferedOutputStream bos 
= new BufferedOutputStream(fos);

            
byte[] b = new byte[2048];
            
int len;
            
while ((len=fis.read(b))>0)
            {
                fos.write(b,
0,len);
                len 
= 0;
            }
        }
        
catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    
public static void bufferCopyUtil(String from,String to){
        
try
        {
            File source 
= new File(from);
            File destiny 
= new File(to);
            FileInputStream fis 
= new FileInputStream(source);
            BufferedInputStream bis 
= new BufferedInputStream(fis);

            
if (!destiny.exists())
            {
                destiny.createNewFile();
            }
            FileOutputStream fos 
= new FileOutputStream(to);
            BufferedOutputStream bos 
= new BufferedOutputStream(fos);

            
int len = 0;
            
byte[] b = new byte[4096];
            
while (true)
            {
                len 
= bis.read(b);
                
if (len<=0)
                {
                    
break;
                }
                bos.write(b,
0,len);
            }
            bos.flush();
            bos.close();
            fos.close();
            bis.close();
            fis.close();
        }
        
catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    
public static void main(final String[] args) 
    {    
        
try
        {
            
long l1 = System.currentTimeMillis();
            bufferCopyUtil(
"FarmFrenzy3.exe","D:/开心农场.exe");
            
long l2 = System.currentTimeMillis();
            
long cost = (l2 - l1)/1000l;
            System.out.println(cost);
        }
        
catch (Exception e)
        {
            e.printStackTrace();
        }
        
/*System.out.println(File.pathSeparator);
        System.out.println(File.pathSeparatorChar);
        System.out.println(File.separator);
        System.out.println(File.separatorChar);
        try
        {
            File f = new File("CalendarTest.java");
            long lastM = f.lastModified();
            Calendar cal = Calendar.getInstance();
            
            cal.setTimeInMillis(lastM);
            Date d = cal.getTime();
            System.out.println(sdf.format(d));
            System.out.println(cal.getMinimalDaysInFirstWeek());
            System.out.println(cal.getMinimum(Calendar.DAY_OF_WEEK));
            System.out.println(cal.getTimeZone().getID());
            File ff = new File("tt/kk");
            File fff = new File("Tir");
            boolean $b = f.mkdir();
            boolean b = ff.mkdirs();
            System.out.println($b);
            System.out.println(b);
            boolean $$b = f.renameTo(fff);
*/

        }
        
/*try
        {
            File file = new File(".");
            String[] fileNames;
            if (args.length==0)
            {
                fileNames = file.list();
            }else{
                fileNames = file.list(new CusFilter(args[0]){
                    class CusFilter implements FilenameFilter
                    {
                        public boolean accept(File f,String name){
                            String s = new File(name).getName();
                            return s.indexOf(args[0])!=-1;
                        }
                    }
                });
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
*/
        
    }
posted on 2010-11-18 15:07 狄浩 阅读(149) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: