溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java中怎么創(chuàng)建以一年中日期命名的文件夾

發(fā)布時(shí)間:2021-08-17 21:39:47 來(lái)源:億速云 閱讀:275 作者:chen 欄目:編程語(yǔ)言

本篇內(nèi)容介紹了“java中怎么創(chuàng)建以一年中日期命名的文件夾”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

java中創(chuàng)建以一年中日期命名的文件夾,拷貝文件(完整代碼)

創(chuàng)建文件夾所在的路徑

C:ftpdirtestfiles

創(chuàng)建文件夾的路徑示例:

C:/ftpdir/testfiles2008-3-3110.10.10.1STAV0100

C:ftpdirtestfiles2008-3-3110.10.10.2STAV0100

所要復(fù)制的文件:

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("測(cè)試開(kāi)始時(shí)間: " + 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("測(cè)試結(jié)束時(shí)間: " + 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();
}

}

///******************刪除文件開(kāi)始********************//

//新建類名: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("測(cè)試開(kāi)始時(shí)間: " + 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("測(cè)試結(jié)束時(shí)間: " + 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("總共用時(shí): " + hour + " 小時(shí)" + minute + " 分" + second + " 秒");
}

//供刪除備份文件調(diào)用的函數(shù) ------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;
}


//將日期字符串轉(zhuǎn)化為長(zhǎng)整形
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;
}


//供刪除備份文件調(diào)用的函數(shù) ------end

}

///******************刪除文件結(jié)束********************//

“java中怎么創(chuàng)建以一年中日期命名的文件夾”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI