android读写文本文件,Android写入日志到文本文件

这篇博客探讨了如何在Android中实现日志记录功能,特别是如何读取现有日志文件的内容并追加新的数据。代码示例展示了如何打开和读取Log.txt文件,然后将新数据写入文件,但目前的实现存在一个问题,即新数据并未成功附加到文件原有内容之后。

我正在尝试写入日志到Android文件自定义Log.txt文件使用Mine的代码,但然后此方法创建文件,但不包含任何内容。基本上我想读取文件的以前的内容,然后附加我的数据与现有的内容。

守则如下:

public static void write(String str)

{

InputStream fileInputStream = null;

FileOutputStream fileOutpurStream = null;

try

{

fileInputStream = new FileInputStream(file);

fileOutpurStream = new FileOutputStream(file);

if(file.exists())

{

int ch = 0;

int current = 0;

StringBuffer buffer = new StringBuffer();

while((ch = fileInputStream.read()) != -1)

{

buffer.append((char) ch);

current++;

}

byte data[]=new byte[(int)file.length()];

fileInputStream.read(data);

fileOutpurStream.write(data);

fileOutpurStream.write(str.getBytes(),0,str.getBytes().length);

fileOutpurStream.flush();

}

else

{

file.createNewFile();

fileOutpurStream.write(str.getBytes(),0,str.getBytes().length);

fileOutpurStream.flush();

}

}

catch(Exception e)

{

e.printStackTrace();

}

finally

{

try

{

fileInputStream.close();

fileOutpurStream.flush();

fileOutpurStream.close();

fileOutpurStream = null;

fileInputStream = null;

}

catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值