0

Hello I started to code my own [Root] file browser in Android, and my application gets folders from command output like this;

drwxr-xr-x root     root              2019-04-26 19:16 .system
drwxr-xr-x root     root              2019-04-26 19:16 acct
drwxrwx--- system   cache             2019-04-29 01:05 cache
dr-x------ root     root              2019-04-26 19:16 config
lrwxrwxrwx root     root              2019-04-26 19:16 d -> /sys/kernel/debug
drwxrwx--x system   system            2019-04-26 19:16 data
-rw-r--r-- root     root          482 1970-01-01 02:00 default.prop
drwxr-xr-x root     root              2019-04-26 19:16 dev
drwxrwx--x radio    system            2019-04-26 19:16 efs
lrwxrwxrwx root     root              2019-04-26 19:16 etc -> /system/etc
-rw-r--r-- root     root        84113 1970-01-01 02:00 file_contexts
-rw-r----- root     root          922 1970-01-01 02:00 fstab.goldfish

For example I want only get size and name from column 4 and 7 how could I get them in java? thanks for everyone.

Zoe
  • 27,060
  • 21
  • 118
  • 148
E. Kolver
  • 103
  • 1
  • 7
  • Did you try using a regular expression? – Bastida Apr 28 '19 at 23:27
  • What a regular expression? I'm new to java and I make this in two weeks ago with full java. (I used file and baseadapter for listview) it was working but not mounting or open a system folder like /data/ or /system/. and I started to rewrite my codes so first I need to get files from ls command as a string like in the first message but I don't how I get only * name and size. and thanks for answering – E. Kolver Apr 28 '19 at 23:41
  • Regular expression are used in many languages and can give you part of a string if it has the format you declared in the expresion. Here is an example that could help you: https://stackoverflow.com/questions/4662215/how-to-extract-a-substring-using-regex If you still need help ill try to form the regexp but I'm not really good using it :P – Bastida Apr 28 '19 at 23:48

1 Answers1

0

You could use these methods to get the 4th column and the 7th:

private static int getSize(String line) {
    int result = 0;

    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher(line);
    if (m.find()) {
        String temp = m.group();

        Matcher m2 = Pattern.compile("\\d+\\-").matcher(line);
        if (m2.find()) {
            String dateNumber = m2.group().replace("-", "");
            if (!dateNumber.equals(temp)) {
                result = Integer.parseInt(temp);
            }
        }
    }
    return result;
}

private static String getName(String line) {
    String result = "";

    Pattern p = Pattern.compile("[A-Za-z_./\\s>-]+$");
    Matcher m = p.matcher(line);
    if (m.find()) {
        result = m.group();
    }

    return result;
}
Bastida
  • 105
  • 1
  • 9