java io 编码_java io的编码问题

这篇博客介绍了在Java中如何正确读取和写入文件以避免中文乱码问题。通过使用UTF-8编码的InputStreamReader和OutputStreamWriter确保了文件内容的正确编码。在读取文件时,博客提供了详细的代码示例,使用BufferedReader进行逐行读取。而在写入文件时,同样使用UTF-8编码的BufferedWriter完成写操作,确保中文字符正确保存。此外,还讨论了FileWriter的局限性,并提供了一个使用OutputStreamWriter替代FileWriter来创建UTF-8编码文件的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、JAVA读取文件,避免中文乱码。

/**

* 读取文件内容

*

* @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、JAVA写入文件,避免中文乱码。

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();

}

}

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"?>这是测试。");

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();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值