java io的编码问题
2010-06-27 18:22
197 查看
1。http://blog.csdn.net/qingyuexiao/archive/2009/04/21/4097723.aspx
1、J***A读取文件,避免中文乱码。
/**
* 读取文件内容
*
* @param filePathAndName
* String 如 c://1.txt 绝对路径
* @return boolean
*/
public static String readFile(String filePathAndName) {
String fileContent = "";
try {
File f = new File(filePathAndName);
if(f.isFile()&&f.exists()){
InputStreamReader read = new InputStreamReader(new FileInputStream(f),"UTF-8");
BufferedReader reader=new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
fileContent += line;
}
read.close();
}
} catch (Exception e) {
System.out.println("读取文件内容操作出错");
e.printStackTrace();
}
return fileContent;
}
2、J***A写入文件,避免中文乱码。
public static void writeFile(String filePathAndName, String fileContent) {
try {
File f = new File(filePathAndName);
if (!f.exists()) {
f.createNewFile();
}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),"UTF-8");
BufferedWriter writer=new BufferedWriter(write);
//PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filePathAndName)));
//PrintWriter writer = new PrintWriter(new FileWriter(filePathAndName));
writer.write(fileContent);
writer.close();
} catch (Exception e) {
System.out.println("写文件内容操作出错");
e.printStackTrace();
}
}
2.http://tech.ddvip.com/2007-10/119238610036399.html
package cn.yethyeth.sample.io;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
/** *//**
* 本文件名为FileWriterSubstituteSample,实际上是在寻找FileWriter的替代者。
* 因为FileWriter在写文件的时候,其编码方式似乎是System.encoding或者System.file.encoding,
* 在中文win下encoding基本是gb2312,在en的win下基本是iso-8859-1,总之不是utf-8。
* 所以要创建一个utf-8的文件,用FileWriter是不行的。
* 目前不知道如何更改其用来写文件的编码方式,因此对于创建utf-8文件使用如下方式来代替。
*
* 参见:
* http://www.malcolmhardie.com/weblogs/angus/2004/10/23/java-filewriter-xml-and-utf-8/ */
public class FileWriterSubstituteSample ...{
public static void main(String[] args)...{
String path="cn/yethyeth/sample/resources/XML_UTF-8.xml";
try ...{
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(path),"UTF-8");
out.write("<?xml version="1.0" encoding="utf-8"?><a>这是测试。</a>");
out.flush();
out.close();
System.out.println("success...");
} catch (UnsupportedEncodingException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
1、J***A读取文件,避免中文乱码。
/**
* 读取文件内容
*
* @param filePathAndName
* String 如 c://1.txt 绝对路径
* @return boolean
*/
public static String readFile(String filePathAndName) {
String fileContent = "";
try {
File f = new File(filePathAndName);
if(f.isFile()&&f.exists()){
InputStreamReader read = new InputStreamReader(new FileInputStream(f),"UTF-8");
BufferedReader reader=new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
fileContent += line;
}
read.close();
}
} catch (Exception e) {
System.out.println("读取文件内容操作出错");
e.printStackTrace();
}
return fileContent;
}
2、J***A写入文件,避免中文乱码。
public static void writeFile(String filePathAndName, String fileContent) {
try {
File f = new File(filePathAndName);
if (!f.exists()) {
f.createNewFile();
}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),"UTF-8");
BufferedWriter writer=new BufferedWriter(write);
//PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filePathAndName)));
//PrintWriter writer = new PrintWriter(new FileWriter(filePathAndName));
writer.write(fileContent);
writer.close();
} catch (Exception e) {
System.out.println("写文件内容操作出错");
e.printStackTrace();
}
}
2.http://tech.ddvip.com/2007-10/119238610036399.html
package cn.yethyeth.sample.io;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
/** *//**
* 本文件名为FileWriterSubstituteSample,实际上是在寻找FileWriter的替代者。
* 因为FileWriter在写文件的时候,其编码方式似乎是System.encoding或者System.file.encoding,
* 在中文win下encoding基本是gb2312,在en的win下基本是iso-8859-1,总之不是utf-8。
* 所以要创建一个utf-8的文件,用FileWriter是不行的。
* 目前不知道如何更改其用来写文件的编码方式,因此对于创建utf-8文件使用如下方式来代替。
*
* 参见:
* http://www.malcolmhardie.com/weblogs/angus/2004/10/23/java-filewriter-xml-and-utf-8/ */
public class FileWriterSubstituteSample ...{
public static void main(String[] args)...{
String path="cn/yethyeth/sample/resources/XML_UTF-8.xml";
try ...{
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(path),"UTF-8");
out.write("<?xml version="1.0" encoding="utf-8"?><a>这是测试。</a>");
out.flush();
out.close();
System.out.println("success...");
} catch (UnsupportedEncodingException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章推荐
- java io的编码问题
- java io的编码问题
- 跟着老毕学Java之IO之File、Properties及其他流对象与编码问题
- 黑马程序员——java之IO体系中File类、编码问题、对象序列化
- 编码问题之:java.io.UTFDataFormatException: Invalid byte 2 of 2-byte UTF-8 sequence.
- 编码问题之:java.io.UTFDataFormatException: Invalid byte 2 of 2-byte UTF-8 sequence.
- Java-IO的编码解码问题
- 编码问题之:java.io.UTFDataFormatException: Invalid byte 2 of 2-byte UTF-8 sequence.
- java io输出的编码问题
- jsp,java中文乱码问题另类解决方案,jni加密、解密编码
- 问题三20150724java.io.InvalidClassException:local class incompatible: stream classdesc
- Java应用中的编码问题
- Java编码问题汇总
- Java反转字符串和相关字符编码问题
- Java反转字符串和相关字符编码问题
- 深入分析 Java 中的中文编码问题
- java.net.URLEncode编码 与 URLDecode解码问题 %E5%9B%9B%E5%B7%9D
- Java get post 编码问题解析
- ajax提交编码转换问题 java.io.charConVersionException:EOF
- 编码问题(.java/.jsp/.js等文件的中文乱码)