java文件读写乱码

renlei413326889 2010-10-12 11:14:47
java文件读写最近碰到乱码问题,以前没有注意过,今天才发现,先贴代码:
public class FileReadOp {
public static void main(String[] args) {
FileInputStream fileInput = null;
try {
/**
* 在普通的Java工程中建立了一个test.txt文本,
* 内容:"test我喜欢深圳"
*/
fileInput = new FileInputStream("test.txt");
//缓冲区以前是new byte[1024],今天故意改成5
byte[] buffer = new byte[5];

String test = "test我喜欢深圳";
byte[] testBytes = test.getBytes();
System.out.println("文本内容总字节数:" + testBytes.length);

//计数,缓冲区很小,while循环次数统计
int count = 0;
int number = 0;
while((number = fileInput.read(buffer)) > 0){
System.out.println(new String(buffer,0,number));
System.out.println("循环次数:" + ++count);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
fileInput.close();
fileInput = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

结果输出:
文本内容总字节数:14
test我
循环次数:1
喜欢深
循环次数:2

循环次数:3

因为以前使用java文件操作在网上和收集的视频中看到的示例代码基本上缓冲区设置都很大,而操作的文本文件内容又很少,加上自己没去细究,今天偶然的操作让我很意外。
一个中文占两个字节,我在代码中缓冲区大小设置为5,文件读取时第一次填充缓冲区的内容为"test"加上一个中文的一半,估计这就是乱码的原因。就算把缓冲区设置很大,假设在不知情的情况下,文件过大,依然会有乱码问题的出现。
我想知道大家处理文件操作的时候通用且安全的做法是怎样的?希望大家能够提出自己的见解!
...全文
441 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
XJAVASunjava 2010-10-24
  • 打赏
  • 举报
回复
个人觉得楼主完全没有必要给自己找不自在,有简单的解决方法为什么要选择一个麻烦的方法,字符流就是为了补充字节流的不足而设计的..完成任务才是目的
水中影子 2010-10-24
  • 打赏
  • 举报
回复
推荐用字符流来解决中文
望舒 2010-10-24
  • 打赏
  • 举报
回复
System.Text.Encoding enc = null;

using (System.IO.FileStream file = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
if (file.CanSeek)
{
byte[] bom = new byte[4]; // Get the byte-order mark, if there is one file.Read(bom, 0, 4);

if(bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
{
enc = System.Text.Encoding.UTF8;
}
else if (bom[0] == 0xff && bom[1] == 0xfe)
{
// ucs-2le, ucs-4le, and ucs-16le
enc=System.Text.Encoding.Unicode;
}
else if(bom[0] == 0xfe && bom[1] == 0xff)
{
// utf-16 and ucs-2
enc=System.Text.Encoding.BigEndianUnicode;
}
else if(bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff)
{
// ucs-4
enc = System.Text.Encoding.UTF32;
}
else
{
enc = System.Text.Encoding.ASCII;
}
}
else
{
// The file cannot be randomly accessed, so you need to decide what to set the default to
// based on the data provided. If you're expecting data from a lot of older applications,
// default your encoding to Encoding.ASCII. If you're expecting data from a lot of newer
// applications, default your encoding to Encoding.Unicode. Also, since binary files are
// single byte-based, so you will want to use Encoding.ASCII, even though you'll probably
// never need to use the encoding then since the Encoding classes are really meant to get
// strings from the byte array that is the file.
enc = System.Text.Encoding.ASCII;
}
return enc;
}


不要骂人啊,本人很懒
czmchen 2010-10-13
  • 打赏
  • 举报
回复
读取文本很少人用字节流操作
java.io.BufferedInputStream可以设置编码,可以设置编码还有什么不可以做的?
amossavez 2010-10-13
  • 打赏
  • 举报
回复
FileWriter fileWrite=new FileWriter("test.txt");
BufferedWriter bufferWrite=new BufferedWriter(fileWrite);
bufferWrite.write("test我喜欢深圳!");
bufferWrite.flush();
雕虫大计 2010-10-13
  • 打赏
  • 举报
回复
判斷字符編碼的範圍,
renlei413326889 2010-10-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 renlei413326889 的回复:]

我知道可以用字符流可以很好的解决,但是我想用字符流来操作,谢谢你的解答
[/Quote]

我想用字节流操作,打快了
renlei413326889 2010-10-12
  • 打赏
  • 举报
回复
我知道可以用字符流可以很好的解决,但是我想用字符流来操作,谢谢你的解答
「已注销」 2010-10-12
  • 打赏
  • 举报
回复
用字符流

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧