Android的日志写入文本文件 [英] Android Writing Logs to text File
本文介绍了Android的日志写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写使用我这个code日志自定义log.txt文件在Android上的文件,但随后这个方法创建的文件,但包含什么。基本上,我想读的文件previous内容,然后与现有的内容追加我的数据。
在code是如下:
公共静态无效写入(字符串str)
{
InputStream中的FileInputStream = NULL;
FileOutputStream中fileOutpurStream = NULL;
尝试
{
的FileInputStream =新的FileInputStream(文件);
fileOutpurStream =新的FileOutputStream(文件);
如果(file.exists())
{
INT CH = 0;
INT电流= 0;
StringBuffer的缓冲区=新的StringBuffer();
而((CH = fileInputStream.read())!= - 1)
{
buffer.append((char)的CH);
当前++;
}
字节的数据[] =新的字节[(INT)file.length()];
fileInputStream.read(数据);
fileOutpurStream.write(数据);
fileOutpurStream.write(str.getBytes(),0,str.getBytes()的长度。);
fileOutpurStream.flush();
}
其他
{
file.createNewFile();
fileOutpurStream.write(str.getBytes(),0,str.getBytes()的长度。);
fileOutpurStream.flush();
}
}
赶上(例外五)
{
e.printStackTrace();
}
最后
{
尝试
{
fileInputStream.close();
fileOutpurStream.flush();
fileOutpurStream.close();
fileOutpurStream = NULL;
的FileInputStream = NULL;
}
赶上(IOException异常E)
{
// TODO自动生成的catch块
e.printStackTrace();
}
}
解决方案
希望这可以帮助...
公共无效appendLog(字符串文本)
{
文件日志文件=新的文件(SD卡/ log.file);
如果(!logFile.exists())
{
尝试
{
logFile.createNewFile();
}
赶上(IOException异常E)
{
// TODO自动生成的catch块
e.printStackTrace();
}
}
尝试
{
// BufferedWriter将性能,真正的设置附加到文件标志
BufferedWriter将BUF =新的BufferedWriter(新的FileWriter(日志文件,真));
buf.append(文本);
buf.newLine();
buf.close();
}
赶上(IOException异常E)
{
// TODO自动生成的catch块
e.printStackTrace();
}
}
I'm Trying to Write Logs to Custom Log.txt File on Android File using this code of Mine but then this method creates file but contains nothing. Basically I want to read previous contents of the file and then append my data with the existing content.
The Code is as follows :
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();
}
}
解决方案
Hope this can help...
public void appendLog(String text)
{
File logFile = new File("sdcard/log.file");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这篇关于Android的日志写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文