JAVA中Fileinputstream的read问题

来源:百度知道 编辑:UC知道 时间:2024/06/14 19:35:37
int x;
byte y[]=new byte[1024];

try
{

File file=new File("文档.txt");
FileInputStream fileread=new FileInputStream(file);
while((x=fileread.read(y,0,1024))!=-1)
{

String s=new String(y);

}

}

catch(IOException e)
{

}

问题是: 为什么byte[]数组长度无论设成2,512,1024等都会出现一些乱码,好像文件文字越多,要设成的大小就要越大,请问要怎样设置才可以无论文件多大,都可以正确显示出文本全部内容啊? 还有请解释一下这个错误原因~ 请高手们来帮忙~

你用的哪个byte[]数组的意思是做缓冲器啊
那你怎么不把BufferedInputStream这个用上啊
是缓冲器啊!!!加在你的
FileInputStream fileread=new FileInputStream(file);
BufferedInputStream buff=new BufferedInputStream(fileread);
while((x=buff.read(y,0,1024))!=-1)