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

被折叠的 条评论
为什么被折叠?



