java中建立以一年中日期命名的資料夾,複製檔案 刪除檔案(完整程式碼)

shwenwen發表於2009-03-31

java中建立以一年中日期命名的資料夾,複製檔案(完整程式碼)

建立資料夾所在的路徑

C:ftpdirtestfiles

建立資料夾的路徑示例:

C:/ftpdir/testfiles2008-3-3110.10.10.1STAV0100

C:ftpdirtestfiles2008-3-3110.10.10.2STAV0100

所要複製的檔案:

C:ftpdirtestfilesbasicfile.txt

[@more@]

package file;

import java.io.*;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class CreateFileTest
{
public CreateFileTest()
{
}

public static void main(String[] args) throws IOException, Exception
{
int ipOne = 10; //10~17
int ipFour = 1; //1~255
String testHomeDir = "C:ftpdirtestfiles";
String date = "";
String ip = "";

String testBeginTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
System.out.println("測試開始時間: " + testBeginTime);

File resourceFile = new File(testHomeDir + "");
for(int i = -365; i < 0; i++)
{
GregorianCalendar calendar;
calendar = new GregorianCalendar();
calendar.add(GregorianCalendar.DATE, i);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
date = year + "-" + month + "-" + day;

for(ipOne = 10; ipOne <= 17; ipOne++)
{
ipFour = 1;
for(ipFour = 1; ipFour <= 255; ipFour++)
{
ip = ipOne + ".10.10." + ipFour;
StringBuffer filepath = new StringBuffer("");
filepath.append(testHomeDir + date + "" + ip + "");
File file = new File(filepath.toString());
if(!file.exists())
{
file.mkdirs();
}

// C:ftpdirtestfiles2008-3-3110.10.10.1STAa.txt
File objFile = new File(filepath.toString() + "");
objFile.createNewFile();
CopyFile(resourceFile, objFile);

//操作STAvt010目錄 STAvt010
String vt010Path = filepath.toString() + "";
File vt010File = new File(vt010Path);
if(!vt010File.exists())
{
vt010File.mkdirs();
}

objFile = null;
for(int j = 1; j < 11; j++)
{
objFile = new File(vt010File.getAbsolutePath() + "" + j + "_a.txt");
objFile.createNewFile();
CopyFile(resourceFile, objFile);
}
}
}
}

String testEndTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
System.out.println("測試結束時間: " + testEndTime);
}

public static void CopyFile(File in, File out) throws Exception
{
FileInputStream fis = new FileInputStream(in);
FileOutputStream fos = new FileOutputStream(out);
byte[] buf = new byte[1024];
int i = 0;
while((i = fis.read(buf)) != -1)
{
fos.write(buf, 0, i);
}
fis.close();
fos.close();
}

}

///******************刪除檔案開始********************//

//新建類名:DelFile

package file;

import java.io.File;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.StringTokenizer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.*;

public class DelFile
{
public DelFile()
{
}

public static void main(String[] args)
{
long a = System.currentTimeMillis();

int ipOne = 10; //10~17
int ipFour = 1; //1~255

String ip = "";
int count = 0;
String absConfigPath = "F:ftpdirtestfiles7Dayconfigdata";

//String absConfigPath="/export/home0/wenwei/ftpdir/testfiles/7Day/configdata/2009-3-31";
String delDate = "2009-3-25";

String testBeginTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
System.out.println("測試開始時間: " + testBeginTime);

ArrayList subPathBeforDelDatelist = getSubPathBeforDelDate(absConfigPath, delDate);

for(int i = 0; i < subPathBeforDelDatelist.size(); i++)
{
String tempPath = (String)subPathBeforDelDatelist.get(i);
System.out.println("刪除日期檔案路徑:"+tempPath);
for(ipOne = 10; ipOne < 18; ipOne++)
{
ipFour = 1;
for(ipFour = 1; ipFour <= 255; ipFour++)
{
ip = ipOne + ".10.10." + ipFour;
count++;
System.out.println("ip=" + ip);
File file = new File(tempPath + File.separator + ip);
if(file.exists())
{
removeFile(file);
}

//刪除以日期命名的空資料夾
if(isNullFolder(tempPath))
{
removeFile(tempPath);
}
}
}
}

System.out.println("count=" + count);
String testEndTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
long b = System.currentTimeMillis();
System.out.println("測試結束時間: " + testEndTime);
long hour = (b - a) / 3600000;
long minute = (b - a) / (60 * 1000) - hour * 60;
long second = (b - a) / 1000 - hour * 60 * 60 - minute * 60;
System.out.println("總共用時: " + hour + " 小時" + minute + " 分" + second + " 秒");
}

//供刪除備份檔案呼叫的函式 ------begin
//找出小於指定日期的子路徑,返回List 元素值為 C:ftpdirconfigdata2009-3-9
public static ArrayList getSubPathBeforDelDate(String strPath, String strDate)
{
ArrayList tempList=new ArrayList();
File dir = new File(strPath);
File[] files = dir.listFiles();
if(files == null)
{
return tempList;
}

for(int i = 0; i < files.length; i++)
{
if(files[i].isDirectory())
{
String strFileName = files[i].getAbsolutePath();
if(pathContains(strFileName, strDate))
{
tempList.add(files[i].getAbsolutePath());
//System.out.println("files[i].getAbsolutePath()=" + files[i].getAbsolutePath());
}

}
}
return tempList;
}

//判斷檔案路徑中是否包含的日期小於等於指定日期
public static boolean pathContains(String path, String strDelDate)
{
if(path == null || strDelDate == null)
{
return false;
}
if(path == "" || strDelDate == "")
{
return false;
}

boolean flag = false;
int datePos = path.lastIndexOf(File.separator);
String strDate = path.substring(datePos + 1);
if(convertDatetoLong(strDelDate) - convertDatetoLong(strDate) >= 0)
{
flag = true;
}
return flag;
}


//將日期字串轉化為長整形
public static long convertDatetoLong(String strDate)
{
long result = 0L;
SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd");
try
{
Date date = dateFormate.parse(strDate);
result = date.getTime();
}
catch(ParseException ex)
{
}
return result;
}

//刪除檔案
public static void removeFile(ArrayList filelist)
{
String path = "";
if(filelist != null && filelist.size() > 0)
{
for(int i = 0; i < filelist.size(); i++)
{
path = (String)filelist.get(i);
removeFile(path);
}
}
}

public static void removeFile(String path)
{
removeFile(new File(path));
}

//刪除某一路徑下所有檔案,然後刪除父空資料夾
public static void removeFile(File path)
{
if(path.isDirectory())
{
File[] child = path.listFiles();
if(child != null && child.length != 0)
{
for(int i = 0; i < child.length; i++)
{
removeFile(child[i]);
child[i].delete();
}
}
}
path.delete();
}

//判斷某一資料夾是否是空資料夾
public static boolean isNullFolder(String path)
{
boolean flag = false;
if(path == null || path.equals(""))
{
return false;
}
File filepath = new File(path);
if(filepath.isDirectory())
{
File[] child = filepath.listFiles();
if(child == null || child.length == 0)
{
flag = true;
}
else
{
flag = false;
}
}
else
{
flag = false;
}
return flag;
}


//供刪除備份檔案呼叫的函式 ------end

}

///******************刪除檔案結束********************//

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/143526/viewspace-1019645/,如需轉載,請註明出處,否則將追究法律責任。

相關文章