我的代码解密文件夹中的所有加密文件,但我使用了Thread.sleep()。如果我不使用Thread.sleep();程序无法解密所有文件。我正在寻找更好的方式来解密没有Thread.sleep()的所有文件。Decrypting files via Runtime.getRuntime()。exec without Thread.sleep()
我需要在代码中写入条件,其中此代码正在等待文件被解密。你可以帮帮我吗?
我的代码:
public static void decrypt() throws InterruptedException, IOException {
File folder = new File("c:/Users/hajdukri/Desktop/src");
File[] files = folder.listFiles();
String pass;
Scanner in = new Scanner(System.in);
System.out.println("Enter a password");
pass = in.nextLine();
if("pass123456".equals(pass)){
for(File a :files){
Thread.sleep(500);
Runtime.getRuntime().exec("cmd /C start C:\\Users\\hajdukri\\Documents\\NetBeansProjects\\JavaApplication3\\gpg.bat");
if (a.isFile()) {
String fileName = a.getName();
// get only only last index (exeption)
int co = fileName.lastIndexOf('.')+1;
StringBuilder sb = new StringBuilder(fileName);
sb.delete(0, co);
sb.toString();
if ("gpg".equals(sb.toString())){
int last = fileName.lastIndexOf('.');
StringBuilder delLast = new StringBuilder(fileName);
delLast.delete(last, 4+last);
delLast.toString();
File file = new File("C:\\Users\\hajdukri\\Documents\\NetBeansProjects\\JavaApplication3\\gpg.bat");
String content = "cd C:\\Users\\hajdukri\\Desktop\\src \r\ngpg --output "+ delLast +" --batch --passphrase "+"\""+pass+"\""+ " "+" --decrypt "+ fileName +"\r\nexit b/";
FileWriter fw = new FileWriter(file.getAbsoluteFile());
try (BufferedWriter bw = new BufferedWriter(fw)) {
bw.write(content);
bw.close();
}
}
}
}
}
else{
System.out.println("Invalid password entered!");}
}
我只需要在以前的文件decripted下一个文件运行调用Runtime.getRuntime()EXEC(gpg.bat)。
非常感谢。
Runtime.getRuntime()。exec()返回一个Process对象。使用对象waitFor()方法等待,直到你的子进程完成。 –
我使用了waitFor(),但代码无法正常工作。我试图解密文件夹中的5个文件,但该程序只解密4个,有时只有3个文件。 – Xhyzors
将调试状态代码添加到您的代码中,以确切查看它卡住的位置。如果你有时说它解密到4个文件,最后一个失败可能会出现问题。 –