2013-02-17 1 views
0

busybox가있는 기기가 실행 중입니다. 없는 공백와 폴더 또는 파일이 제대로 이동 만 (공백과 폴더 출력은busybox mv (공백 포함)가 자동으로 실패 함

mv -v "/storage/sdcard0/media/music/Progressive Death Metal" "/storage/sdcard0/Music" 

없음 MV 출력, 방법은 그냥 "false"를 반환 제대로

public static boolean mv(File source, File target) { 
    if (!source.exists() || !target.exists()) { 
     return false; 
    } 
    try { 
     StringBuilder command = new StringBuilder("mv -v "); 
     command.append('\"'); 
     command.append(source.getCanonicalPath()); 
     command.append('\"'); 
     command.append(' '); 
     command.append('\"'); 
     command.append(target.getCanonicalPath()); 
     command.append('\"'); 
     System.out.println(command.toString()); 
     Process process = Runtime.getRuntime().exec(command.toString()); 
     StringBuilder output = new StringBuilder(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
     String line; 
     while ((line = in.readLine()) != null) { 
      output.append(line); 
     } 
     System.out.println(output.toString()); 
     return process.waitFor() == 0; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

를 이동하지 않는 것이 아닌 것 같다 -zero 종료 코드).

정식 경로를 사용해야합니까, 아니면 절대 경로를 사용하여 셸에 그대로 둘 수 있습니까?

편집

나는 또한 파일 이름은 따옴표가 있다면, 인수가 잘못 될 것이라고 와서, 그래서 나는이

같은 이스케이프 문자

private static String getCommandLineString(String input) { 
    return input.replace("\\", "\\\\") 
      .replace("\"", "\\\"") 
      .replace("\'", "\\\'") 
      .replace("`", "\\`") 
      .replace(" ", "\\ "); 
} 

그리고 지금 MV 외모를 추가하는 방법을 만들어 내가 무엇을 얻을

public static boolean mv(File source, File target) { 
    if (!source.exists() || !target.exists()) { 
     return false; 
    } 
    try { 
     StringBuilder command = new StringBuilder("mv -v "); 
     command.append(getCommandLineString(source.getAbsolutePath())); 
     command.append(' '); 
     command.append(getCommandLineString(target.getAbsolutePath())); 
     System.out.println(command.toString()); 
     Process process = Runtime.getRuntime().exec(command.toString()); 
     StringBuilder output = new StringBuilder(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
     String line; 
     while ((line = in.readLine()) != null) { 
      output.append(line); 
     } 
     System.out.println(output.toString()); 
     return process.waitFor() == 0; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

입니다

하지만 조용한 0이 아닌 종료 코드가 표시됩니다.

답변

0

마지막으로 작동했습니다. Exec은 쉘을 요청해야하며, OutputStream은 명령을 작성해야합니다.

private static boolean execute(boolean superuser, String command) { 
    DataOutputStream os = null; 
    try { 
     Process process = Runtime.getRuntime().exec(superuser ? "su" : "sh"); 
     os = new DataOutputStream(process.getOutputStream()); 
     os.writeBytes(command + "\n"); 
     os.flush(); 

     os.writeBytes("exit\n"); 
     os.flush(); 

     return process.waitFor() == 0; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (os != null) { try { os.close(); } catch (Exception e) {} } 
    } 
    return false; 
} 

public static boolean mv(File source, File target) { 
    if (!source.exists() || !target.exists()) { 
     return false; 
    } 
    try { 
     StringBuilder command = new StringBuilder("mv "); 
     command.append(getCommandLineString(source.getAbsolutePath())); 
     command.append(' '); 
     command.append(getCommandLineString(target.getAbsolutePath())); 
     return execute(false, command.toString()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 
}