package com.yd.bpm.utils;
import java.io.File;
import java.util.List;
public class FileSerialNumber{
private static String prefix = "6999";
private FileSerialNumber() {}
private static FileSerialNumber single=null;
//静态工厂方法
public static FileSerialNumber getInstance() {
if (single == null) {
single = new FileSerialNumber();
}
return single;
}
public String getNumber(File file , int start) {
int num = start;
synchronized(single){
if (file.exists()) {
List<String> list = FileUtil.readList(file);
num = Integer.parseInt(list.get(0));
}
FileUtil.rewrite(file, String.valueOf(num + 1));
}
return prefix + num;
}
}
package com.yd.bpm.utils;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.text.Format;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class FileUtil {
public static void rewrite(File file, String data) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
bw.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static List<String> readList(File file) {
BufferedReader br = null;
List<String> data = new ArrayList<String>();
try {
br = new BufferedReader(new FileReader(file));
for (String str = null; (str = br.readLine()) != null;) {
data.add(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data;
}
}
首先引入上面的两个类,然后调用:
FileSerialNumber.getInstance().getNumber(new File("e:\duty.txt"),100);
说明:getNumber这个方法的两个参数第一个是代表本地磁盘路径第二值是有序编码的起始值;如果磁盘路径的文件中存在编码就会根据这个编码有序增加,如果没有就会根据第二个参数的值进行有序增加