[英]How to get specific parts from columns in String in JAVA
您好我開始在Android中編寫自己的[Root]文件瀏覽器,我的應用程序從命令輸出中獲取文件夾,就像這樣;
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
例如,我只想從第4列和第7列獲取大小和名稱,如何在java中獲取它們? 謝謝大家。
您可以使用這些方法獲得第4列和第7列:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.