FileStream读取与java写入数据库乱码乱码如何解决

filestream的write方法写入数字乱码
[问题点数:20分,结帖人sinat_]
filestream的write方法写入数字乱码
[问题点数:20分,结帖人sinat_]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2017年2月 总版技术专家分月排行榜第三
2018年1月 .NET技术大版内专家分月排行榜第一2017年5月 .NET技术大版内专家分月排行榜第一2017年4月 .NET技术大版内专家分月排行榜第一2017年3月 .NET技术大版内专家分月排行榜第一2017年2月 .NET技术大版内专家分月排行榜第一2016年10月 .NET技术大版内专家分月排行榜第一2016年8月 .NET技术大版内专家分月排行榜第一2016年7月 .NET技术大版内专家分月排行榜第一
2017年3月 .NET技术大版内专家分月排行榜第三2017年2月 .NET技术大版内专家分月排行榜第三2016年9月 .NET技术大版内专家分月排行榜第三2016年8月 .NET技术大版内专家分月排行榜第三2016年7月 .NET技术大版内专家分月排行榜第三2016年3月 .NET技术大版内专家分月排行榜第三2016年1月 .NET技术大版内专家分月排行榜第三2015年12月 .NET技术大版内专家分月排行榜第三2015年11月 .NET技术大版内专家分月排行榜第三
2018年3月 总版技术专家分月排行榜第一2013年5月 总版技术专家分月排行榜第一
2018年4月 总版技术专家分月排行榜第二2016年7月 总版技术专家分月排行榜第二2016年3月 总版技术专家分月排行榜第二2015年12月 总版技术专家分月排行榜第二2014年8月 总版技术专家分月排行榜第二2014年7月 总版技术专家分月排行榜第二2013年6月 总版技术专家分月排行榜第二
匿名用户不能发表回复!|C# StreamReader和StreamWriter读取和写入汉字出现乱码的解决方法 - 流星落 - 博客园
随笔 - 512, 文章 - 0, 评论 - 6, 引用 - 0
(转贴)注意:汉字使用GB2312编码
测试页面代码:
using Susing System.Collections.Gusing System.T
namespace StreamReaderAndStreamWriter{class Program{static void Main(string[] args){Console.WriteLine("读取文本文件内容");Console.Write("输入文本文件所在目录:");string sBaseFile = Console.ReadLine();StreamReaderAndStreamWriter.ReadFile(sBaseFile);Console.WriteLine("追加文件");StreamReaderAndStreamWriter.AppendText(sBaseFile);}}}类页面代码:
using Susing System.Collections.Gusing System.Tusing System.IO;
namespace StreamReaderAndStreamWriter{class StreamReaderAndStreamWriter{//读取文本文件public static void ReadFile(string sFile){if (File.Exists(sFile)){//获取一个文件流对象,用于读写文件FileStream fs = File.OpenRead(sFile);//获取一个指向文件流的流读取器StreamReader sr = new StreamReader(fs,Encoding.GetEncoding("gb2312"));//以gb2312编码读取文本文件中的汉字,要不然,读取的内容中如果有汉字,则显示为乱码。//读取所有文本内容string data = sr.ReadToEnd();//关闭对象,释放资源sr.Close();fs.Close();Console.WriteLine(string.Format("读取文件&&{0}", sFile));Console.WriteLine(data);}elseConsole.WriteLine(string.Format("{0}不存在",sFile));}//追加文本文件public static void AppendText(string sFile){if (File.Exists(sFile)){//编辑文本文件Console.WriteLine("输入写入内容");string swrite = Console.ReadLine();//获取一个指向文件流的流编辑器StreamWriter sw = new StreamWriter(sFile, true, Encoding.GetEncoding("gb2312"));//这里很重要,看一下这个StreamWriter()格式就一目了然了,sFile声明了文本对象;true声明了可以进行Appedn;Encoding.GetEncoding("gb2312")声明了一GB2312编码向文本文件写入内容,这样就可以避免写入汉字出现乱码。sw.Write(swrite);//关闭对象,释放资源sw.Close();//fs.Close();Console.WriteLine("向{0}中追加文件",sFile);}elseConsole.WriteLine("{0}不存在",sFile);}}}Java读取、写入文件如何解决乱码问题
转载 &发布时间:日 15:56:09 & 投稿:lijiao
这篇文章主要介绍了Java读取、写入文件如何解决乱码问题,需要的朋友可以参考下
读取文件流时,经常会遇到乱码的现象,造成乱码的原因当然不可能是一个,这里主要介绍因为文件编码格式而导致的乱码的问题。首先,明确一点,文本文件与二进制文件的概念与差异。
文本文件是基于字符编码的文件,常见的编码有ASCII编码,UNICODE编码、ANSI编码等等。二进制文件是基于值编码的文件,你可以根据具体应用,指定某个值是什么意思(这样一个过程,可以看作是自定义编码。)
因此可以看出文本文件基本上是定长编码的(也有非定长的编码如UTF-8)。而二进制文件可看成是变长编码的,因为是值编码嘛,多少个比特代表一个值,完全由你决定。
&对于二进制文件,是千万不能使用字符串的,因为字符串默认初始化时会使用系统默认编码,然而,二进制文件因为自定义编码自然与固定格式的编码会有所冲突,所以对于二进制的文件只能采用字节流读取、操作、写入。
&&对于文本文件,因为编码固定,所以只要在读取文件之前,采用文件自身的编码格式解析文件,然后获取字节,再然后,通过指定格式初始化字符串,那么得到的文本是不会乱码的。虽然,二进制文件也可以获取到它的文本编码格式,但是那是不准确的,所以不能同日而语。
具体操作如下:
1)获取文本文件的格式
public static String getFileEncode(String path) {
String charset ="asci";
byte[] first3Bytes = new byte[3];
BufferedInputStream bis =
boolean checked =
bis = new BufferedInputStream(new FileInputStream(path));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1)
if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
charset = "Unicode";//UTF-16LE
} else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
charset = "Unicode";//UTF-16BE
} else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF) {
charset = "UTF8";
bis.reset();
if (!checked) {
int len = 0;
int loc = 0;
while ((read = bis.read()) != -1) {
if (read &= 0xF0)
if (0x80 &= read && read &= 0xBF) //单独出现BF以下的,也算是GBK
if (0xC0 &= read && read &= 0xDF) {
read = bis.read();
if (0x80 &= read && read &= 0xBF)
//双字节 (0xC0 - 0xDF) (0x80 - 0xBF),也可能在GB编码内
} else if (0xE0 &= read && read &= 0xEF) { //也有可能出错,但是几率较小
read = bis.read();
if (0x80 &= read && read &= 0xBF) {
read = bis.read();
if (0x80 &= read && read &= 0xBF) {
charset = "UTF-8";
//TextLogger.getLogger().info(loc + " " + Integer.toHexString(read));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
bis.close();
} catch (IOException ex) {
private static String getEncode(int flag1, int flag2, int flag3) {
String encode="";
// txt文件的开头会多出几个字节,分别是FF、FE(Unicode),
// FE、FF(Unicode big endian),EF、BB、BF(UTF-8)
if (flag1 == 255 && flag2 == 254) {
encode="Unicode";
else if (flag1 == 254 && flag2 == 255) {
encode="UTF-16";
else if (flag1 == 239 && flag2 == 187 && flag3 == 191) {
encode="UTF8";
encode="asci";// ASCII码
2)通过文件的编码格式读取文件流
* 通过路径获取文件的内容,这个方法因为用到了字符串作为载体,为了正确读取文件(不乱码),只能读取文本文件,安全方法!
public static String readFile(String path){
String data =
// 判断文件是否存在
File file = new File(path);
if(!file.exists()){
// 获取文件编码格式
String code = FileEncode.getFileEncode(path);
InputStreamReader isr =
// 根据编码格式解析文件
if("asci".equals(code)){
// 这里采用GBK编码,而不用环境编码格式,因为环境默认编码不等于操作系统编码
// code = System.getProperty("file.encoding");
code = "GBK";
isr = new InputStreamReader(new FileInputStream(file),code);
// 读取文件内容
int length = -1 ;
char[] buffer = new char[1024];
StringBuffer sb = new StringBuffer();
while((length = isr.read(buffer, 0, 1024) ) != -1){
sb.append(buffer,0,length);
data = new String(sb);
}catch(Exception e){
e.printStackTrace();
log.info("getFile IO Exception:"+e.getMessage());
if(isr != null){
isr.close();
} catch (IOException e) {
e.printStackTrace();
log.info("getFile IO Exception:"+e.getMessage());
3)通过文件指定的格式写入文件
* 按照指定的路径和编码格式保存文件内容,这个方法因为用到了字符串作为载体,为了正确写入文件(不乱码),只能写入文本内容,安全方法
* @param data
将要写入到文件中的字节数据
* @param path
文件路径,包含文件名
* @return boolean
当写入完毕时返回
public static boolean writeFile(byte data[], String path , String code){
boolean flag =
OutputStreamWriter osw =
File file = new File(path);
if(!file.exists()){
file = new File(file.getParent());
if(!file.exists()){
file.mkdirs();
if("asci".equals(code)){
code = "GBK";
osw = new OutputStreamWriter(new FileOutputStream(path),code);
osw.write(new String(data,code));
osw.flush();
}catch(Exception e){
e.printStackTrace();
log.info("toFile IO Exception:"+e.getMessage());
if(osw != null){
osw.close();
}catch(IOException e){
e.printStackTrace();
log.info("toFile IO Exception:"+e.getMessage());
4)对于二进制文件而且内容很少的,例如Word文档等,可以使用如下方式读取、写入文件
* 从指定路径读取文件到字节数组中,对于一些非文本格式的内容可以选用这个方法
* @param path
文件路径,包含文件名
* @return byte[]
文件字节数组
public static byte[] getFile(String path) throws IOException {
FileInputStream stream=new FileInputStream(path);
int size=stream.available();
byte data[]=new byte[size];
stream.read(data);
stream.close();
* 把字节内容写入到对应的文件,对于一些非文本的文件可以采用这个方法。
* @param data
将要写入到文件中的字节数据
* @param path
文件路径,包含文件名
* @return boolean isOK 当写入完毕时返回
* @throws Exception
public static boolean toFile(byte data[], String path) throws Exception {
FileOutputStream out=new FileOutputStream(path);
out.write(data);
out.flush();
out.close();
以上就是本文的全部内容,希望对大家的学习有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具扫一扫体验手机阅读
读取文件,解决中文乱码问题
<span type="1" blog_id="535985" userid='
分享到朋友圈
关注作者,不错过每一篇精彩&#xe621; 上传我的文档
&#xe602; 下载
&#xe60c; 收藏
粉丝量:86
该文档贡献者很忙,什么也没留下。
&#xe602; 下载此文档
C# StreamReader 和 StreamWriter 读取和写入汉字出现乱码的解决方法
下载积分:100
内容提示:C# StreamReader 和 StreamWriter 读取和写入汉字出现乱码的解决方法
文档格式:PDF|
浏览次数:492|
上传日期: 05:36:29|
文档星级:&#xe60b;&#xe60b;&#xe60b;&#xe60b;&#xe60b;
全文阅读已结束,如果下载本文需要使用
&#xe71b; 100 积分
&#xe602;下载此文档
该用户还上传了这些文档
C# StreamReader 和 StreamWriter 读取和写入汉字出
关注微信公众号

我要回帖

更多关于 java写入文件乱码 的文章

 

随机推荐