public static void createCSV(List list,
String filePath,
String fileName,
String separator)
throws Exception {
// CSV弌椡弶傔儘僌
if (log.isInfoEnabled()) {
log.info(Constants.MSGCI003);
}
String sep = DEFAULT_SEPARATOR;
OutputStream os = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
// CSV偵彂偔堦峴偺僨乕僞
String line = "";
List innerList = new ArrayList();
// CSV弌椡儕僗僩僨乕僞偑側偄応崌偼張棟懕峴晄壜擻
if (list == null) {
return;
}
// 嬫愗傝暥帤巜掕偑偁傟偽桪愭偡傞
if (separator != null && !"".equals(separator)) {
sep = separator;
}
try {
File file = new File(filePath);
// 僷僗偺憂寶偟偰偄側偄応崌
if (!file.exists()) {
// 僷僗偺憂寶偡傞
boolean flag = file.mkdirs();
if (!flag) {
// CSV僼傽僀儖僷僗Exception
if (log.isInfoEnabled()) {
log.error(Constants.MSGCI007);
}
return;
}
}
fileName = filePath + "//" + fileName;
os = new FileOutputStream(fileName, true);
osw = new OutputStreamWriter(os);
bw = new BufferedWriter(osw);
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getClass() == Object[].class) {
line = objectArrToCSVString((Object[]) list.get(i), sep);
bw.write(line);
bw.newLine();
} else if (list.get(i).getClass() == ArrayList.class) {
innerList = (ArrayList) list.get(i);
// csv
line = objectArrToCSVString(innerList.toArray(), sep);
// 1峴弌椡
bw.write(line);
bw.newLine();
}
}
// 僼儔僢僔儏
bw.flush();
} finally {
if (bw != null) {
// 廔椆張棟
bw.close();
}
}
// CSV弌椡廔傢傞儘僌
if (log.isInfoEnabled()) {
log.info(Constants.MSGCI004);
}
}
private static String objectArrToCSVString(Object[] objArr,
String sep) {
String returnStr = "";
if (objArr != null) {
for (int i = 0; i < objArr.length; i++) {
// 嬫愗傝暥帤偺憓擖
if (0 < i) {
returnStr = returnStr + sep;
}
// CSV梡偺僨乕僞偵惍宍偟偰捛壛
returnStr += objectToCSVString(objArr[i]);
}
}
return returnStr;
}
private static String objectToCSVString(Object obj) {
String returnStr = "";
if (obj != null) {
returnStr = (String) obj;
if (returnStr.equals("")) {
returnStr = "/"/"";
} else {
// ["]偼[""]偵曄峏偡傞乮僄僗働乕僾乯
returnStr = returnStr.replaceAll("/"", "/"/"");
int length = returnStr.length();
if (0 < length) {
for (int i = 0; i < length; i++) {
char c = returnStr.charAt(i);
if (c == ',') {
returnStr = "/"" + returnStr + "/"";
break;
}
}
}
}
}
return returnStr;
}
}
随即数字
int randoms = (int)(Math.random() * temp + Integer.parseInt(strDown));