文件操作

读文件:
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();
}
初次读写文件可能遇到的问题:最开始读文件没有该文件,导致初始化出错

转载于:https://www.cnblogs.com/ct1104/p/3757787.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值