public static boolean softLinkMode(String localPath, String softPath){
Process p;
int status;
try {
long time = System.currentTimeMillis();
// String cmd = "ln -s " + localPath + " "+ softPath; (文件名没有空格)
String[] cmd ={ "ln","-s",localPath,softPath}; //(有空格时候,使用字符数组)
p = Runtime.getRuntime().exec(cmd);
releaseProcessStream(p);
status = p.waitFor();
if (status == 0) {
return true;//success
} else {
return false;
}
}catch (Exception e) {
return false;
}
}
private static void releaseProcessStream(Process p) throws IOException{
InputStream stderr = p.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null)
System.out.println(line);
}
可以通过context的getFilesDir().getAbsolutePath()方法获取对应app所在的/data/data目录
今天再把怎么删除上次建立的软连接也附上来:
public static void cleansoftLink(String path) {
Process p;
int status;
try {
long time = System.currentTimeMillis();
// String cmd = "ln -s " + localPath + " "+ webPath;
String[] cmd = { "rm", "-rf", path};
p = Runtime.getRuntime().exec(cmd);
releaseProcessStream(p);
status = p.waitFor();
if (status == 0) {
LogUtils.e("tlh", TAG + "deleteD:" + path + ", status:"+status);
}
} catch (Exception e) {
LogUtils.e("tlh", TAG + "deleteD error:" +e.getMessage());
}
}