读文件:
public String readfile(String file_name)
{
String content=null;
try
{
FileInputStream fis=openFileInput(file_name);
ByteArrayOutputStream lds=new ByteArrayOutputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1)
{
lds.write(buf, 0, len);
}
content=lds.toString();
fis.close();
lds.close();
// Toast.makeText(this,new String(buf),Toast.LENGTH_SHORT).show();
// Toast.makeText(this, content,Toast.LENGTH_SHORT).show();


}catch(Exception e)
{
e.printStackTrace();
}
return content;
}
@SuppressLint("ParserError")
写文件:
public void writefile(String file_name,String[] group,int group_len)
{
String content=group[0];
for(int i=1;i<group_len;i++)
content=content+'\n'+group[i];
content=content+'\n';
try
{
FileOutputStream fos=openFileOutput(file_name,Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.flush();
fos.close();
}catch(Exception e)
{
e.printStackTrace();
}
// Toast.makeText(this, content,Toast.LENGTH_SHORT).show();
}
初次读写文件可能遇到的问题:最开始读文件没有该文件,导致初始化出错

posted on 2014-05-28 20:01  CT1104  阅读(166)  评论(0编辑  收藏  举报