昨天读取txt文档的时候始终出现中文乱码,去好多网站看了,都TMD一坑,根本没有解决问题嘛!大多数博客上写的的格式就是一样的不晓得哪个超哪个,最后终于在http://blog.csdn.net/maxracer/article/details/5436580上找到解决办法,进行一些改进后,得到可行的方法
1.Read.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
public class Read {
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
// 一次读n个字节
int n=100;
byte[] tempbytes = new byte[n];
int byteread = 0;
in = new FileInputStream(fileName);
Read.showAvailableBytes(in);//查看剩余字节数
while ((byteread = in.read(tempbytes)) != -1) {
System.out.print(new String(tempbytes, 0, byteread, "GB2312"));
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
*/
public static void readFileByChars(String filePath) {
File file = new File(filePath);
Reader reader = null;
try {
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(file),"GB2312");
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
// 一次读多个字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(filePath),"GB2312");
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars)) != -1) {
// 同样屏蔽掉\r不显示
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String filePath) {
File file = new File(filePath);
BufferedReader reader = null;
try {
//以行为单位读取文件内容,一次读一整行
//reader = new BufferedReader(new FileReader(file));//换成下面两句
InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"gb2312");
reader=new BufferedReader(isr);
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
//tempString=new String(tempString.getBytes("iso-8859-1"),"gb2312");//如果要这段代码,则上面设置的“gb2312”不要
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 随机读取文件内容
*/
public static void readFileByRandomAccess(String filePath) {
RandomAccessFile randomFile = null;
try {
//随机读取一段文件内容
// 打开一个随机访问文件流,按只读方式
randomFile = new RandomAccessFile(filePath, "r");//filePath换成new File(filePath)也可以
// 文件长度,字节数
long fileLength = randomFile.length();
// 读文件的起始位置
int beginIndex = (fileLength > 4) ? 4 : 0;
// 将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
// 将一次读取的字节数赋给byteread
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.print(new String(bytes, 0, byteread,"GB2312"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}
/**
* 显示输入流中还剩的字节数
*/
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("当前字节输入流中的字节数为:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.Start.java
public class Start {
public static void main(String[] args) {
String filePath = "F:/txt/tsxk1.txt";
Read.readFileByBytes(filePath);
Read.readFileByChars(filePath);
Read.readFileByLines(filePath);
Read.readFileByRandomAccess(filePath);
}
}
读取txt文件没问题,其他文件有待改进。。。
分享到:
相关推荐
Java读取TXT文本文件乱码解决方案.doc
VB读取记事本无乱码并赋值给string型变量 VB读取记事本无乱码并赋值给string型变量
彻底解决JAVA读取写入 TXT中文乱码问题 源码
c++实现txt文件的读写,并解决txt编码格式为UTF-8显示乱码问题。
解决C#语言在读写包含汉字的txt文件时乱码的问题!!!
VB.NET读取文本(解决乱码),实用性代码。
彻底解决android读取中文txt的乱码(自动判断文档类型并转码)
2、当TXT文件内容包含中文时,有时会显示乱码,解决方法:打开TXT文件,在“另存为”对话框中选择“UTF-8”格式); 3、当TXT文件名为中文时,无法读取,页面会显示空白内容; 4、经测试,只有搜狗告诉浏览器可以...
1、读取本地JSON ,但是显示汉字乱码 public static String readLocalJson(Context context, String fileName){ String jsonString=; String resultString=; try { BufferedReader bufferedReader=new ...
原创matlab读取txt中文文本的解决方案-程序.rar matlab对中文的支持一直以来都是个不小的问题,尤其在读取文本方面。一般读出的中文文本都是乱码,网上找了半天没找到答案,自己研究了一下,在快要绝望的时候终于...
java读取txt文本中如含有中文,可能会出现乱码,解决方案是: 1.要统一编码,java工程的编码,txt文本编码,java工程中的java文本编码都统一为utf-8; 2.利用 InputStreamReader(new FileInputStream(fileUrl), ...
本文实例讲述了C#读取中文文件出现乱码的解决方法。分享给大家供大家参考。具体分析如下: 先来看这段代码: FileStream aFile = new FileStream(SingleFile,FileMode.Open); StreamReader sr = new StreamReader(a...
本文实例讲述了PHP读取文件,解决中文乱码UTF-8的方法。分享给大家供大家参考,具体如下: $opts = array( 'file' => array( 'encoding' => "utf-8" ) ); $opts = array('http' => array('encoding' => 'utf-8'))...
选择文件夹,将指定文件里面的所有特定后缀的UTF-8编码文件转成GB2312文件,使用VS2013 ,MFC 开发.
后台给一个txt文件,编码是utf-8,在Mac电脑Xcode开发环境下读取txt文件内容,汉字会出现乱码,英文没有乱码这种情况。 二、尝试解决方法: 修改编码格式,尝试了NSUTF16StringEncoding,NSUTF8StringEncoding,...
基于labview2011开发的,使用操作系统自带的编码.net转换UTF8编码,实测从未出错,比LabVIEW自带...可用于中转,使得labview可以上传繁体数据到服务器,或解析服务器发送的繁体字数据,或读取UTF8编码的繁体字txt文档。
主要介绍了Java 解决读写本地文件中文乱码的问题的相关资料,需要的朋友可以参考下
当需要将文本文件导入SAS中时,UTF-8编码的中文有可能会乱码,此文讨论了一种解决方法
通常我们电脑里的txt文件都是ASNI编码,放到手机,MP3等设备里面,打开看到的都是乱码。所以我们必须先把它们转换为Unicode格式,才能够在手机和MP3上面打开。如果有大量的文件,一个一个打开另存为Unicode,那是很...