您好,登錄后才能下訂單哦!
本篇內容介紹了“java中怎么創建以一年中日期命名的文件夾”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
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 + "basicfile.txt");
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 + "STA");
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() + "a.txt");
objFile.createNewFile();
CopyFile(resourceFile, objFile);
//操作STAvt010目錄 STAvt010
String vt010Path = filepath.toString() + "V0100";
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
}
///******************刪除文件結束********************//
“java中怎么創建以一年中日期命名的文件夾”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。