关于读文件的问题

liangqi2008 2010-07-27 06:50:01
各位大大,我写了一个读txt文件的小程序,但是现在我发现他会重复的读取文件内容,不清楚为什么,请各位多多指教
代码如下:
public void read(String filename )
{
FileInputStream fIn = null;
InputStreamReader isr = null;
int i=0;
char[] inputBuffer = new char[255];
String data;
File file = new File(filename);
String encoding = getFileEncode(filename);
try{
fIn=new FileInputStream(file);
isr = new InputStreamReader(fIn,encoding);
i=isr.read(inputBuffer);
data = new String(inputBuffer,0,i);
if(data!=null){
while(i!=-1){
i=isr.read(inputBuffer);
data = data+new String(inputBuffer);
}
fileread.setText(data);
}
//else
// fileread.setText(data);


}
catch (Exception e) {
e.printStackTrace();
fileread.setText("can not open the file\n");
}
finally
{
try {
isr.close();
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
...全文
38 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
SmallHero007 2010-07-27
  • 打赏
  • 举报
回复


public void read(String filename )
{
FileInputStream fIn = null;
InputStreamReader isr = null;
int i=0;
char[] inputBuffer = new char[255];
String data;
File file = new File(filename);
String encoding = getFileEncode(filename);
try{
fIn=new FileInputStream(file);
isr = new InputStreamReader(fIn,encoding);
i=isr.read(inputBuffer);
data = "";

while(i!=-1){
i=isr.read(inputBuffer);
data = data+new String(inputBuffer);
}
fileread.setText(data);
}
catch (Exception e) {
e.printStackTrace();
fileread.setText("can not open the file\n");
}
finally
{
try {
isr.close();
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

楼主看看这样行不行?

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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