网站建设资讯

NEWS

网站建设资讯

java中怎么创建以一年中日期命名的文件夹

本篇内容介绍了“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中怎么创建以一年中日期命名的文件夹”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


网站题目:java中怎么创建以一年中日期命名的文件夹
网站地址:http://cdweb.net/article/gphoje.html