相关疑难解决方法(0)

Android写入日志文本文件

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

守则如下:

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 = …
Run Code Online (Sandbox Code Playgroud)

android

119
推荐指数
9
解决办法
18万
查看次数

记录到Android上的文件

有没有办法从Android手机中检索日志消息.

我正在构建一个使用我的HTC Hero的GPS的应用程序.我可以从eclipse运行和调试应用程序,但这不是一个很好的GPS用例,坐在我的办公桌前.

当我四处走动时,当我开启应用程序时,我会出现间歇性异常.无论如何,我可以将这些例外输出到SD卡上的文本文件或输出Log.x("")到文本文件的调用,以便我可以看到异常是什么.

谢谢

编辑:解决方案

这是我最终使用的代码......

Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {

    PrintWriter pw;
    try {
        pw = new PrintWriter(
                new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true));
        ex.printStackTrace(pw);
        pw.flush();
        pw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
});
Run Code Online (Sandbox Code Playgroud)

我不得不包裹这条线

pw = new PrintWriter(new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true));
Run Code Online (Sandbox Code Playgroud)

在try/catch中,因为Eclipse不会让我编译应用程序.它不停地说

Unhandled exception type IOException

1 quick fix
    Sorround with try/catch
Run Code Online (Sandbox Code Playgroud)

所以我做了,这一切都很好,但我确实让我想知道Eclipse是关于什么...

logging android

26
推荐指数
3
解决办法
3万
查看次数

如何在Android上配置java.util.logging?

我想在Android上使用java.util.logging.我想使用logging.properties配置日志记录系统.但是如何使用特定的配置文件告诉Android?例如,我将logging.properties放在应用程序的类路径根目录中.Android如何知道logging.properties的位置.

谢谢

logging android

24
推荐指数
2
解决办法
2万
查看次数

如何将我的日志输出从logcat重定向到Android设备上的SD卡?

我正在尝试将我的应用程序的日志重定向到sdcard文件.但我没有这样做.我正在尝试这样的事情.

String cmd= "logcat -v time   ActivityManager:W  myapp:D  *:* >\""+file.getAbsolutePath()+"\"";
Runtime.getRuntime().exec(cmd);
Run Code Online (Sandbox Code Playgroud)

我也尝试了-f选项,但它也没有用.

logging android

18
推荐指数
2
解决办法
3万
查看次数

标签 统计

android ×4

logging ×3