📄 createcsvfile.java
字号:
package org.xk.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CreateCSVFile {
private static CreateCSVFile _createCSVFile = null;
private FileOutputStream _fos = null;
private StringBuffer _sBuffer = null;
private String _path = "";
public static final String DEL_CHAR = ",";
public static final String AV_CHAR = "\"";
public CreateCSVFile() {
_path = "D:\\\\StudentScore.csv";
this.init(_path);
}
/**
*
* @param path
* create csv file's path
*/
public void init(String path) {
String method = "createCSVFile_init";
if (path == null || path.trim().equals("")) {
_path = "D:\\\\StudentScore.csv";
} else {
_path = path;
}
try {
_fos = new FileOutputStream(_path, false);
_sBuffer = new StringBuffer();
System.out.println("create csv file sccuessful");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("not found exception of create csv file");
}
}
/**
* this method is append date in csv file
*
* @param data
*/
public void setData(Object data) {
_sBuffer.append(AV_CHAR);
_sBuffer.append(data);
_sBuffer.append(AV_CHAR);
_sBuffer.append(DEL_CHAR);
}
public void writeLine() {
if (_sBuffer.charAt(_sBuffer.length() - 1) == ',')
_sBuffer.delete(_sBuffer.length() - 1, _sBuffer.length());
_sBuffer.append("\r\n");
}
/**
* this method is close fileoutputstream
*/
public void close() {
String method = "close";
try {
if (_sBuffer != null) {
_fos.write(_sBuffer.toString().getBytes());
System.out.println("close fileOutputStream successful");
} else {
System.out.println("null point exception");
}
} catch (IOException e) {
System.out.println("close fileOutputStream failure");
} finally {
try {
if (_fos != null) {
_fos.close();
_fos = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -