?? updatefile.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 + -