哪位高手能提供一段编号自动增加的代码? 编号的规则是年月日+00001,比如2005071800001,当天的自动增加,每天从00001开始,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这样还不如直接用日期生成,时间到秒不就可以了如果用每天的编号的话,有两种做法,一种是设一个静态变量在系统里,写一个获取id的方法时间比较,然后如果是另一天的话,id清零,不然id+1;另一种放在数据库里,做法相同 把文件的编号分成两部分YYYYMMdd + XXXXX。我觉得主要是后面的编号不好生成,前面的编号比较容易生成用Calendar类就可以了,我的想法是建立一个临时的temp.txt文件用来存放当前的日期YYYYMMdd,在每次生成新编号时用temp.txt文件的值和当前的日期进行比较,如果不相等的话XXXXX置零,并且重写temp.txt文件中存放的值为当前日期;如果相等的话XXXXX++就行了。最后把两部分编号加起来就OK了。不知道有没有更为简单的方法啊?! static String getCurrentDate() { java.util.Calendar now = java.util.Calendar.getInstance(); java.text.SimpleDateFormat yearAndMonth = new java.text.SimpleDateFormat("yyyy-MM-dd"); String retString = yearAndMonth.format(now.getTime()); return retString; } private static Object lock = new Object(); public static void main(String[] args) throws Exception{ String logFile = "c:\\lastFile.txt"; File lastFile = new File(logFile); String strFileName = ""; BufferedWriter file = null; BufferedReader input = null; boolean firstUsed = false; try { synchronized (lock) { if (!lastFile.exists()) { lastFile.createNewFile(); firstUsed = true; } if (firstUsed){ try{ file = new BufferedWriter(new FileWriter(logFile)); file.write(getCurrentDate() + "00000"); } catch (Exception e){ System.out.println(e); } finally{ file.close(); } } firstUsed = false; input = new BufferedReader(new FileReader(logFile)); String strNo=input.readLine(); strNo = strNo.substring(strNo.length()-5); strNo="00000"+String.valueOf(Integer.parseInt(strNo)+1); strNo=strNo.substring(strNo.length()-5); //---------得到结果 strFileName = getCurrentDate()+strNo; file = new BufferedWriter(new FileWriter(logFile)); file.write(strFileName); } } catch (Exception e) { System.out.println(e); }finally{ file.close(); input.close(); }} 用getTime()不就行了?只要系统时间不错,文件就绝对不会重名而且可以支持一天超过10万个文件同时比较文件的新旧之类也很方便 修改了一下: static String getCurrentDate() { java.util.Calendar now = java.util.Calendar.getInstance(); java.text.SimpleDateFormat yearAndMonth = new java.text.SimpleDateFormat("yyyy-MM-dd"); String retString = yearAndMonth.format(now.getTime()); return retString; } private static Object lock = new Object(); public static void main(String[] args) throws Exception{ String logFile = "c:\\lastFile.txt"; File lastFile = new File(logFile); String strFileName = ""; BufferedWriter file = null; BufferedReader input = null; boolean firstUsed = false; try { synchronized (lock) { if (!lastFile.exists()) { lastFile.createNewFile(); firstUsed = true; } String strCurrDate = getCurrentDate(); if (firstUsed){ try{ file = new BufferedWriter(new FileWriter(logFile)); file.write(strCurrDate + "00000"); } catch (Exception e){ System.out.println(e); } finally{ file.close(); } } firstUsed = false; input = new BufferedReader(new FileReader(logFile)); String strNo=input.readLine(); if (strCurrDate.equals(strNo.substring(0,strNo.length()-5))){ strNo = strNo.substring(strNo.length()-5); strNo="00000"+String.valueOf(Integer.parseInt(strNo)+1); strNo=strNo.substring(strNo.length()-5); //---------得到结果 strFileName = strCurrDate+strNo; } else { //---------得到结果 strFileName = strCurrDate+"00001"; } file = new BufferedWriter(new FileWriter(logFile)); file.write(strFileName); } } catch (Exception e) { System.out.println(e); }finally{ file.close(); input.close(); } java 逻辑题 ZipInputStream报java.lang.IllegalArgumentException java可以不使用odbc连接相对目录下的access数据库吗? 求一个正则表达式 如何通过字符串来生成Date类型的对象 谁能帮忙解决下这个问题?关于文件读取和排序的问题 对于 程序 运行的结果 出现的顺寻不是很理解 jdk1.3下,不能运行程序,请解决! 图片上传的问题-急!在线等待 如何在编译完成后将jb6生成的jar文件输出到weblogic下的指定目录?应如何配置jb6? 给大家支个招 支持者有分!! 小问题
如果用每天的编号的话,
有两种做法,一种是设一个静态变量在系统里,
写一个获取id的方法
时间比较,
然后如果是另一天的话,id清零,不然id+1;
另一种放在数据库里,做法相同
我觉得主要是后面的编号不好生成,前面的编号比较容易生成用Calendar类就可以了,我的想法是建立一个临时的temp.txt文件用来存放当前的日期YYYYMMdd,在每次生成新编号时用temp.txt文件的值和当前的日期进行比较,如果不相等的话XXXXX置零,并且重写temp.txt文件中存放的值为当前日期;如果相等的话XXXXX++就行了。
最后把两部分编号加起来就OK了。
不知道有没有更为简单的方法啊?!
java.util.Calendar now = java.util.Calendar.getInstance();
java.text.SimpleDateFormat yearAndMonth =
new java.text.SimpleDateFormat("yyyy-MM-dd");
String retString = yearAndMonth.format(now.getTime());
return retString;
}
private static Object lock = new Object();
public static void main(String[] args) throws Exception{
String logFile = "c:\\lastFile.txt";
File lastFile = new File(logFile);
String strFileName = "";
BufferedWriter file = null;
BufferedReader input = null;
boolean firstUsed = false;
try {
synchronized (lock)
{
if (!lastFile.exists()) {
lastFile.createNewFile();
firstUsed = true;
}
if (firstUsed){
try{
file = new BufferedWriter(new FileWriter(logFile));
file.write(getCurrentDate() + "00000");
} catch (Exception e){
System.out.println(e);
} finally{
file.close();
}
}
firstUsed = false;
input = new BufferedReader(new FileReader(logFile));
String strNo=input.readLine();
strNo = strNo.substring(strNo.length()-5);
strNo="00000"+String.valueOf(Integer.parseInt(strNo)+1);
strNo=strNo.substring(strNo.length()-5);
//---------得到结果
strFileName = getCurrentDate()+strNo;
file = new BufferedWriter(new FileWriter(logFile));
file.write(strFileName);
}
} catch (Exception e) {
System.out.println(e);
}finally{
file.close();
input.close();
}
}
而且可以支持一天超过10万个文件同时比较文件的新旧之类也很方便
static String getCurrentDate() {
java.util.Calendar now = java.util.Calendar.getInstance();
java.text.SimpleDateFormat yearAndMonth =
new java.text.SimpleDateFormat("yyyy-MM-dd");
String retString = yearAndMonth.format(now.getTime());
return retString;
}
private static Object lock = new Object();
public static void main(String[] args) throws Exception{
String logFile = "c:\\lastFile.txt";
File lastFile = new File(logFile);
String strFileName = "";
BufferedWriter file = null;
BufferedReader input = null;
boolean firstUsed = false;
try {
synchronized (lock)
{
if (!lastFile.exists()) {
lastFile.createNewFile();
firstUsed = true;
}
String strCurrDate = getCurrentDate();
if (firstUsed){
try{
file = new BufferedWriter(new FileWriter(logFile));
file.write(strCurrDate + "00000");
} catch (Exception e){
System.out.println(e);
} finally{
file.close();
}
}
firstUsed = false;
input = new BufferedReader(new FileReader(logFile));
String strNo=input.readLine();
if (strCurrDate.equals(strNo.substring(0,strNo.length()-5))){
strNo = strNo.substring(strNo.length()-5);
strNo="00000"+String.valueOf(Integer.parseInt(strNo)+1);
strNo=strNo.substring(strNo.length()-5);
//---------得到结果
strFileName = strCurrDate+strNo; } else {
//---------得到结果
strFileName = strCurrDate+"00001";
}
file = new BufferedWriter(new FileWriter(logFile)); file.write(strFileName); }
} catch (Exception e) {
System.out.println(e);
}finally{
file.close();
input.close();
}