利用Java调用shell脚本读取,shell代码:
##shell的大概流程就是先获取文件夹下所有文件名
##然后写出到单个文件
##然后我们通过Java修改相应的文件夹路径即可
Folder_A="文件夹路径"
tmp="/sdcard/tmp1"
good="-"
for file_a in ${Folder_A}/* dotemp_file=`basename $file_a`
echo $temp_file$good>>$tmp
done
这里我封装了一个Java模块,嫌麻烦的可以自己复制粘贴,然后调用模块:
//简单的文件读写模块,记得更名为io
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
public class io
{
public static String read(String files) throws FileNotFoundException, UnsupportedEncodingException, IOException{
File f=new File(files);
FileInputStream fip=new FileInputStream(f);
InputStreamReader reader=new InputStreamReader(fip,"UTF-8");
StringBuffer sb = new StringBuffer();
while (reader.ready()) {
sb.append((char) reader.read());
}
reader.close();
return sb.toString();
}
public static void write(String txt,String files) throws FileNotFoundException, UnsupportedEncodingException, IOException{
File f=new File(files);
FileOutputStream fip=new FileOutputStream(f);
OutputStreamWriter writer = new OutputStreamWriter(fip, "UTF-8");
writer.append(txt);
writer.close();
}
}
//shell操作模块
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import io;
import java.io.OutputStreamWriter;
public class shell {
public static String run(String com) throws IOException {
String result = null;
Process ret=Runtime.getRuntime().exec(com);
BufferedInputStream in = new BufferedInputStream(ret.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String lineStr;
while ((lineStr = br.readLine()) != null) {
result = lineStr;
}
return result;
}
public static void remove(String file) throws IOException {
shell.run("rm -r " + file);
}
public static String[] getFileList(String files,String su) throws IOException {
shell.remove("/sdcard/tmp1");
String Star="Folder_A=\"" + files + "\"\ntest=\"-\"" + "\ntmp=\"/sdcard/tmp1\"\nfor file_a in ${Folder_A}/* \ndo" + "\ntemp_file=`basename $file_a`" + "\necho $temp_file$test>>$tmp" + "\ndone";
io.write(Star,"sdcard/tmp");
shell.run(su+" sh " + "sdcard/tmp");
String sb=io.read("sdcard/tmp1");
String txt=sb.toString().replaceAll("\n","");
String[] list=txt.split("-");
return list;
}
}
Main界面调用方法
import java.util.*;
import java.io.File;
import java.io.IOException;
import shell;
import java.sql.ResultSetMetaData;
public class Main {
public static void main(String[] args) throws IOException{
String[] filelist=shell.getFileList("data/","su -c");//如果不需要读取内部路径可以把第2个选项留空
System.out.println(Arrays.toString(filelist));
}
}
运行测试
//返回的是文件名,自己后期拼接一下即可
[adb, anr, apex, apk, tmp, app, app, asec, app, ephemeral, app, lib, app, private, app, staging, assetsFairu, backup, bootchart, cache, dalvik, cache, data, dpm, drm, extm, fonts, gsi, gsi_persistent_data, incremental, local, lost+found, media, mediadrm, misc, misc_ce, misc_de, miui, mqsas, nfc, ota, ota_package, per_boot, preloads, property, resource, cache, rollback, rollback, history, rollback, observer, server_configurable_flags, ss, system, system_ce, system_de, tombstones, unencrypted, user, user_de, vendor, vendor_ce, vendor_de]
下一期出一下root文件读写