⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 updatefile.java

📁 Java Classic Examples是我买的两本书:《JAVA经典实例》和《java入门经典源代码》里边附送光盘里带的源码
💻 JAVA
字号:
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class UpdateFile 
{
    public static void main(String args[]) throws IOException 
    {
        String fname = "Write1.txt";       //待复制的文件名
        String childdir = "backup";        //子目录名
        new UpdateFile().update(fname,childdir);
    }
    public void update(String fname,String childdir) throws IOException 
    {
        File f1,f2,child;
        f1 = new File(fname);              //当前目录中创建文件对象f1
        child = new File(childdir);        //当前目录中创建文件对象child
        if (f1.exists()) 
        {
            if (!child.exists())           //child不存在时创建子目录
                child.mkdir();
            f2 = new File(child,fname);    //在子目录child中创建文件f2
            if (!f2.exists() ||            //f2不存在时或存在但日期较早时
                 f2.exists()&&(f1.lastModified() > f2.lastModified())) 
                copy(f1,f2);               //复制
            getinfo(f1);
            getinfo(child);
        }
        else
            System.out.println(f1.getName()+" file not found!");
    }
    public void copy(File f1,File f2) throws IOException 
    {                                      //创建文件输入流对象
        FileInputStream  rf = new FileInputStream(f1);
        FileOutputStream  wf = new FileOutputStream(f2);
                                           //创建文件输出流对象
        int count,n=512;
        byte buffer[] = new byte[n];
        count = rf.read(buffer,0,n);       //读取输入流
        while (count != -1)
        {
            wf.write(buffer,0,count);      //写入输出流
            count = rf.read(buffer,0,n);
        }
        System.out.println("CopyFile  "+f2.getName()+" !");
        rf.close();                        //关闭输入流
        wf.close();                        //关闭输出流
    }
    public static void getinfo(File f1) throws IOException 
    {
        SimpleDateFormat sdf;
        sdf= new SimpleDateFormat("yyyy年MM月dd日hh时mm分");
        if (f1.isFile())
            System.out.println("<File>\t"+f1.getAbsolutePath()+"\t"+
              f1.length()+"\t"+sdf.format(new Date(f1.lastModified())));
        else
        {
            System.out.println("<Dir>\t"+f1.getAbsolutePath());
            File[] files = f1.listFiles();
            for (int i=0;i<files.length;i++)
                getinfo(files[i]);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -