java 如何设置文件流的编码格式代码如下, 我设置后还是SJIS格式的.我想问下怎么改能设置成utf-8
publicstaticvoidCreateCsvfiles1(StringtargetPath,Stringfilename,List<JobBean>list)thr...
public static void CreateCsvfiles1(String targetPath, String filename,
List<JobBean> list) throws Exception {
File file = null;
OutputStream out = null;
PrintWriter pw = null;
OutputStreamWriter osw =null;
// PrintWriter pw = new PrintWriter(
// new OutputStreamWriter(
// new FileOutputStream("yourfilepath"),
// "UTF-8"));
try {
file = new File(targetPath + filename + ".csv");
if(file.exists()){
System.out.println("文件目录存在");
}else{
System.out.println("文件目录bu存在");
}
if(file.isFile()){
System.out.println("文件存在");
}else{
System.out.println("存在");
}
out = new FileOutputStream(file);
osw = new OutputStreamWriter(out,"utf-8");
System.out.println(osw+"aaaaaa");
pw = new PrintWriter(osw);
for (int j = 0; j < 6; j++) {
pw.append("TITLE" + ",");
}
for (int i = 0; i < list.size(); i++) {
String operationname = (list.get(i).getPaerationname());
String operationresult = (list.get(i).getOperationresult());
String username = (list.get(i).getUsername());
String userip = (list.get(i).getUereip());
String operationtime = (list.get(i).getOperationtime());
String operationdetail = (list.get(i).getOperationdetil());
pw.append("\n");
pw.append(operationname + ",");
pw.append(operationresult + ",");
pw.append(username + ",");
pw.append(userip + ",");
pw.append(operationtime + ",");
pw.append(operationdetail + ",");
}
} catch (Exception e) {
System.out.println(e.getMessage());
// TODO: handle exception
} finally {
System.out.println(targetPath + filename + ".csv");
pw.flush();
pw.close();
out.flush();
out.close();
osw.close();
}
} 展开
List<JobBean> list) throws Exception {
File file = null;
OutputStream out = null;
PrintWriter pw = null;
OutputStreamWriter osw =null;
// PrintWriter pw = new PrintWriter(
// new OutputStreamWriter(
// new FileOutputStream("yourfilepath"),
// "UTF-8"));
try {
file = new File(targetPath + filename + ".csv");
if(file.exists()){
System.out.println("文件目录存在");
}else{
System.out.println("文件目录bu存在");
}
if(file.isFile()){
System.out.println("文件存在");
}else{
System.out.println("存在");
}
out = new FileOutputStream(file);
osw = new OutputStreamWriter(out,"utf-8");
System.out.println(osw+"aaaaaa");
pw = new PrintWriter(osw);
for (int j = 0; j < 6; j++) {
pw.append("TITLE" + ",");
}
for (int i = 0; i < list.size(); i++) {
String operationname = (list.get(i).getPaerationname());
String operationresult = (list.get(i).getOperationresult());
String username = (list.get(i).getUsername());
String userip = (list.get(i).getUereip());
String operationtime = (list.get(i).getOperationtime());
String operationdetail = (list.get(i).getOperationdetil());
pw.append("\n");
pw.append(operationname + ",");
pw.append(operationresult + ",");
pw.append(username + ",");
pw.append(userip + ",");
pw.append(operationtime + ",");
pw.append(operationdetail + ",");
}
} catch (Exception e) {
System.out.println(e.getMessage());
// TODO: handle exception
} finally {
System.out.println(targetPath + filename + ".csv");
pw.flush();
pw.close();
out.flush();
out.close();
osw.close();
}
} 展开
1个回答
展开全部
OutputStreamWriter里指定charset应该就是这个用法,你确定List<JobBean>里的那些字符串都是正确的?你可以先作一个单元测试,自己在本地生成一些正确的JobBean,以保证问题不出在其他地方。
追问
搞定了.xieie
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询