因为用java调用系统cmd执行exe文件加密数据时,exe文件里面有IO处理。然后接着马上调用File文件的读取功能,所以出现了File文件句柄未释放问题而无法读取。
这也就是问题所在:执行系统cmd,要等待它结束才能进行IO操作。
//字节流写入
OutputStream out =new FileOutputStream(f); String str= "127.0.0.1"; byte[] b=str.getBytes(); out.write(b); out.close();
String jmtxt = "D:"+File.separator+"加密后的文件2.txt"
Runtime run = Runtime.getRuntime(); Process p = null; try { String command = "exe文件x"+ "加密文件f"+ " "+ "加密后文件jmtxt"; p = run.exec(command); //执行cmd指令代码 p.waitFor(); //就是这一步等待完成 } catch (Exception e) { e.printStackTrace(); } //读取txt文件 InputStream in=new FileInputStream(jmtxt); byte[] c=new byte[1024]; in.read(c); in.close(); System.out.println(new String(c));