java txt

来源:互联网 发布:safari windows 下载 编辑:程序博客网 时间:2024/04/29 16:45
package cn.com.glxt.fifle;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;


import org.apache.log4j.Logger;


import cn.com.glxt.common.FileUtil;


public class CreateTxt {
private static final Logger LOG = Logger.getLogger(CreateTxt.class);
public static void main(String[] args) {
String directory = "D:/Users/test/txt";
String pathname = directory+File.separator+UUID.randomUUID()+".dat";
FileUtil.mkDir(directory);
File txtFile = FileUtil.mkFile(pathname);
writeTxt(txtFile, 2,true);
}

private static void writeTxt(File file,int rows,boolean isAppend){
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file,isAppend);
StringBuffer sbf = new StringBuffer();
for(int i=0;i<rows;i++){
sbf.append(getContent()).append("\r\n");
if(rows%500 ==0){
fos.write(sbf.toString().getBytes("UTF-8"));
sbf.delete(0, sbf.length());
}
}
if(sbf.length() >0){
fos.write(sbf.toString().getBytes("UTF-8"));
}
} catch (FileNotFoundException e) {
LOG.error("找不到文件"+e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
LOG.error("字节码转换错误"+e.getMessage(), e);
} catch (IOException e) {
LOG.error("IO错误"+e.getMessage(), e);
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
LOG.error("IO错误"+e.getMessage(), e);
}
}
}
}
/**
* 模拟业务数据
* @return
*/
private static String getContent(){
return "10001|20150623|400.25|张三|RMB";
}
}
原创粉丝点击