Java – Android accesses log files from a PC

Android accesses log files from a PC… here is a solution to the problem.

Android accesses log files from a PC

My 6.0.1 app behaves strangely on reboot on 2013 nexus (not root), so I tried writing log files to internal storage. I do socket logging via WiFi connected to the PC, but the problem appears when the power reclamation is there, my application starts when it receives startup completion, so I thought I’d write a log file, so I’m trying:

    String filename="tablet.log";
    File logFile = new File(getFilesDir(), filename);
    p("log file: "+logFile);
    try {
        OutputStream outputStream = new FileOutputStream(logFile);
        p("wrote to log file: ");
    } catch (Exception e) {
    try {
        InputStream inputStream = new FileInputStream(logFile);
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
        String string=bufferedReader.readLine();
        p("read from log file: "+string);
    } catch (Exception e) {
    Logger global=Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    try {
        p("log file get name: "+logFile.getPath());
        Handler handler=new FileHandler(logFile.getPath());
        l.warning("added file handler: "+handler);
    } catch(Exception e) {
        l.warning("file handler caught: "+e);

I seem

to be writing a file and seem to have added a file handler, but I don’t see the file when I use adb :

D:\AndroidStudioProjects\Cb7>adb -s 0a9196e8 shell ls /data/user/0/

Get: OpenDir failed and permission denied

Is there any way to transfer files to PC?


Edit: Jared’s suggestion works: adb shell run-as ls files – files will be displayed!

In addition, adb exec-out run-as cat files/file > file fetches files to the PC.

Looks needed: adb -s %device% shell run-as %package% ls -l/data/data/%package%/files


Files in /data cannot be read by shell users. You need to use the run-as command, which will work as long as the debug build is installed.

You can check if the file exists:

adb shell run-as ls files

To copy files to your

PC, you need to copy the log files to your SD card and then use adb pull to retrieve the files. On Android 5.0+, you can simply do the following:

adb shell exec-out run-as cat files/tablet.log > tablet.log

This should work for previous (and current) Android versions:

> adb shell
$ run-as
$ cp files/tablet.log $EXTERNAL_STORAGE/tablet.log
$ exit
> adb pull /sdcard/tablet.log

Obviously, replace with your package name (

See also: Android adb, retrieve database using run-as

Related Problems and Solutions