求java写入txt解决中文乱码的的写法 求java写入txt解决中文乱码的的写法,不生成C盘,或者电脑里某个硬盘(C,D,E,F,G。。)只写入到项目工程下的某个文件夹下例如。。webRoot/test/testData.txt求高手帮忙。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 System.out.println(System.getProperty("user.dir"));//user.dir 指定了当前的路径 public static void main(String[] args) { BufferedWriter out = null; try{ String filename = System.getProperty("user.dir") + "/1.txt"; out = new BufferedWriter(new FileWriter(filename, true)); out.write("张"); }catch (IOException e) { e.printStackTrace(); // error processing code }finally{ if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }试了一下这个可以,没有乱码 public String getWebParth() { String WebParth = this.getClass().getResource("/").getPath(); int i = WebParth.indexOf("/WEB-INF/"); WebParth = WebParth.substring(0, i); return WebParth; }GeneralJS js = new GeneralJS();String path = js.getWebParth() + "/datajslib/datajslib.js";FileOutputStream os = new FileOutputStream(filePath);String str="你好@中国";os.write(str.getBytes("utf-8"));这个为乱码OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream( path, true), "utf-8"); BufferedWriter bw = new BufferedWriter(osw); for(int i=0;i<10;i++){ System.out.println(new Date().toString()); bw.write("你好@中国"); }这个写不进去 public static void main(String[] args) { BufferedWriter out = null; try{ String filename = System.getProperty("user.dir") + "/1.txt"; out = new BufferedWriter(new FileWriter(filename, true)); out.write("张"); }catch (IOException e) { e.printStackTrace(); // error processing code }finally{ if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } 已经解决问题如下:file.mkdir(); 生成的文件在myeclipse下的编码格式为 ISO18030,改下生成后的文件编码就可以没乱码了 String realPath = "D:/resource/"; //路径 File saveFile = new File(realPath + "文件名称"); if (!saveFile.getParentFile().exists()) { saveFile.getParentFile().mkdirs(); } FileOutputStream out = new FileOutputStream(saveFile, true); out.write(("内容").getBytes("UTF-8")); out.close(); myeclipse8.5 如何配置 struts js设置cookie,servlet读cookie,读不到,急 字符串截取.....解决立马给分 WebSphere配置Oracle数据源,通过JNDI获取数据源出错 404 404 jboss中如何获得当前服务程序的绝对路径(根目录) 大家好, 遇到一个xsl解析xml的问题。 请赐教, 怎么把script插入到xsl中? 紧急求助!hibernate的问题! 问个不是问题的问题 有关java和sql 2000连接的问题 这种翻译是怎么实现的 怎样给复选框赋初值
BufferedWriter out = null;
try{
String filename = System.getProperty("user.dir") + "/1.txt";
out = new BufferedWriter(new FileWriter(filename, true));
out.write("张");
}catch (IOException e) {
e.printStackTrace();
// error processing code
}finally{
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
试了一下这个可以,没有乱码
public String getWebParth() {
String WebParth = this.getClass().getResource("/").getPath();
int i = WebParth.indexOf("/WEB-INF/");
WebParth = WebParth.substring(0, i);
return WebParth;
}
GeneralJS js = new GeneralJS();
String path = js.getWebParth() + "/datajslib/datajslib.js";FileOutputStream os = new FileOutputStream(filePath);
String str="你好@中国";
os.write(str.getBytes("utf-8"));
这个为乱码OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
path, true), "utf-8");
BufferedWriter bw = new BufferedWriter(osw);
for(int i=0;i<10;i++){
System.out.println(new Date().toString());
bw.write("你好@中国");
}
这个写不进去
public static void main(String[] args) {
BufferedWriter out = null;
try{
String filename = System.getProperty("user.dir") + "/1.txt";
out = new BufferedWriter(new FileWriter(filename, true));
out.write("张");
}catch (IOException e) {
e.printStackTrace();
// error processing code
}finally{
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
问题如下:file.mkdir(); 生成的文件在myeclipse下的编码格式为 ISO18030,改下生成后的文件编码就可以没乱码了
String realPath = "D:/resource/"; //路径
File saveFile = new File(realPath + "文件名称");
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
FileOutputStream out = new FileOutputStream(saveFile, true);
out.write(("内容").getBytes("UTF-8"));
out.close();