如何按日期增量生成文件
2015-06-08 23:09
309 查看
最近看到一段代码每隔多少行就重新生成一个TXT文件,同时生成的文件名在按当天的日期增量的,不生成重复的文件名。
之前看见其他人使用了递归调用,这边我自己写了一段代码如下:
import java.io.File;
import java.io.IOException;
public class GetNewFileByDate {
}
之前看见其他人使用了递归调用,这边我自己写了一段代码如下:
import java.io.File;
import java.io.IOException;
public class GetNewFileByDate {
public static void main(String[] args) { String date = "20150608"; String path = "D:\\java-test\\"; File file = new File(path + getFileName(path,date)); try { if(file.createNewFile()){ System.out.println(file.getAbsolutePath() + "创建成功"); }else{ System.out.println(file.getAbsolutePath() + "创建失败"); } } catch (IOException e) { e.printStackTrace(); } } /** * 根据日期获得文件名 * @param date * @return */ public static String getFileName(String path,String date){ int serial = 0; String fileName = date + getSerialByCurrentID(1,serial); //文件存在重新计算 while( (new File(path + fileName)).exists() ){ fileName = date + getSerialByCurrentID(1,++serial); } return fileName; } /** * 获取下一个序列 * @param digit * @param currentSerial * @return */ public static String getSerialByCurrentID(int digit,int currentSerial){ String nextSerial = ""; String zero; int raise = 0; String temp = String.valueOf(++currentSerial); //参数错误都返回序列+1 if(digit < 0 || currentSerial < 0){ nextSerial = temp ; }else{ zero = "0"; raise = digit - temp.length(); for(int i = 0 ;i < raise; i++){ nextSerial += zero; } nextSerial += temp; } return nextSerial; }
}
相关文章推荐
- AngularJS笔记---作用域和控制器
- 关于Qt在子线程中使用QMessageBox的折衷方法
- Android是如何在不同屏幕上适配图片的 -- 或控件大小
- 内容提供
- Android NDK(学习笔记四)—— 在NDK开发中JNI打印Log信息
- POJ - 1274 The Perfect Stall 二分图 最大匹配
- [031] 微信公众帐号开发教程第7篇-文本消息中换行符的使用
- DNX/ASP.NET 5的xUnit入门向导
- 18岁生日
- 第十篇 SQL Server代理使用代理帐户
- 集群负载均衡算法原理
- 黑马程序员——Java基础—GUI
- android顺序广播
- C#超级实用的一种类型—匿名类型
- NOIP 2014 D1T2 -联合权值
- Android模拟器与局域网通信
- Zoj 3469 Food Delivery (DP
- Android入门---下拉刷新上拉加载更多策略的实现
- 第六题
- linux 和 hp 上如何生成core文件