解决方案 »
- 为什么匿名内部类调用的方法内局部变量必须为final
- Swing方面的问题,急!!!
- 基础不好,有人帮忙解释下这是什么原因吗?
- 关于类型转换的一个问T,,,,,,,
- 请教关于字体颜色的问题
- 像vc中那样右上角有一个x的面板有对应的java组件吗?
- 对象的内容可以在被调用的方法中改变,但对象的引用是永远不会改变的。"请大家给解释一下"
- 关于JCreator Pro V3,急!
- 高分求一个现在常用的JAVA编译器及运行平台。
- 如何通过javac命令编译src下多个JAVA文件,并放到对应的bin下对应包下
- public synchronized static 获取序列号怎么会主键冲突
- 为什么将集合变成数组后输出不成功?
版主,我看到你提示那个,去查阅了下资料,现在又把这个小例子进一步改进了下,漏写率降低了,但是没解决漏写的根本问题。
public class demo1
{
public static void main(String[] args) throws IOException, InterruptedException
{
String username = null;
String password = "admin"; for (int j = 1; j <= 10; j++)
{
String path = "F:/passwd" + j;
createFile(path); for (int i = 1; i <= 10; i++)
{
username = "yulei" + i; Runtime r = Runtime.getRuntime();
Process child = r.exec("cmd.exe /c start D:/Apache2/bin/htpasswd.exe -b " + path + " " + username + " " + password);
child.waitFor(); // Thread.sleep(3000); // 这里是我自己的本方法,休眠3秒,因为这个插入根本用不了3秒钟
} readFileLineIs10(path);
} } private static boolean readFileLineIs10(String path) throws IOException
{
int i = 1;
BufferedReader br = new BufferedReader(new FileReader(path));
String line = br.readLine();
while (line != null)
{
i++;
line = br.readLine();
} System.out.println(path + " 的行数为: " + i); return i - 1 == 10;
} // 有就删了创,没就创
private static void createFile(String path) throws IOException
{
File file = new File(path); if (file.exists())
{
file.delete();
} file.createNewFile();
}}我也再看看,那边的就结贴了,这个算是咱SE的基本问题,就不再WEB那边问了。
1、通过返回值判断是否已经执行完成了。(不知道是不是waitfor,或是其他...详阅API)
2、修改htpasswd.exe程序,使其有返回值,如return success(fail)等。然后java程序通过读取返回值来判断是否执行完成!
修改htpasswd.exe行不通的,因为不是这个exe,要是这个了到简单了,因为知道它是用户名和MD5加密了的密码;这个代码模拟的是调用另一个软件里面的一个应用程序,该应用有一个不公开的加密方案,我自己认为可能是MD5(密码+自定义字符串)这样的组合,所以我必须也只能去调用它,没有办法修改这个应用,说白了就是原来这套系统没有用户管理,只有手动一条条生成;现在我自己写一个用户管理,然后调用它的加密写入它的文本,而管理这边就用页面的形式,点点鼠标就完成了,不需要再去教对应的命令行怎么用。
其实并发实际操作时不存在,少数的管理人员,企业里面就3、5个人,不会一同上班的,但是这个确确实实是设计缺陷,所以很想解决掉,觉得以后自己只要在这样单位工作,这样的调用外部东西就不会少。
{
String username = null;
String password = "admin"; for (int j = 1; j <= 10; j++)
{
String path = "F:/passwd" + j;
createFile(path); for (int i = 1; i <= 10; i++)
{
username = "yulei" + i; Runtime r = Runtime.getRuntime();
Process child = r.exec("cmd.exe /c start D:/Apache2/bin/htpasswd.exe -b " + path + " " + username + " " + password);
child.waitFor(); int value = child.exitValue();
System.out.println("子线程的返回值: " + value);
} System.out.println(readFileLineIs10(path));
} }加上看了,返回值是都操作完毕了,返回0;但是可怕的有的文件才只写入3行,到目前为止没有一个完整写入的,这个真没办法接受。子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
F:/passwd1 的行数为: 10
false
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
F:/passwd2 的行数为: 9
false
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
F:/passwd3 的行数为: 9
false
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
F:/passwd4 的行数为: 9
false
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
F:/passwd5 的行数为: 9
false
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
F:/passwd6 的行数为: 9
false
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
F:/passwd7 的行数为: 3
false
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
F:/passwd8 的行数为: 8
false
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
F:/passwd9 的行数为: 9
false
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
子线程的返回值: 0
F:/passwd10 的行数为: 9
false
2、返回值path有没有打印出来?有可能接收不完全导致,你也可以考虑用IO流。
而且自己生成文本,我还可以对这个生成文本进行占用控制,这个还可以解决多个用户同时发出这一指令的并发问题。我去写代码尝试下,2从来没用过的知识点啊!生的很
public class demo2
{
public static void main(String[] args) throws IOException, InterruptedException
{
addMethod(); // checkMethod();
} private static void checkMethod() throws IOException
{
for (int i = 1; i <= 10; i++)
{
String path = "F:/passwd" + i;
System.out.println(readFileLineIs10(path));
}
} private static void addMethod() throws IOException, InterruptedException
{
// 拼接批处理文件内容
StringBuffer stringBuffer = new StringBuffer();
for (int j = 1; j <= 10; j++)
{
String path = "F:/passwd" + j;
createFile(path); for (int i = 1; i <= 10; i++)
{
stringBuffer.append("D:/Apache2/bin/htpasswd.exe -b ").append(path).append(" ").append("yulei" + i).append(" ").append("admin").append("\n");
}
}
stringBuffer.append("exit"); String path = "f:/1.bat";
File file = createFile(path); // 写文件
BufferedWriter bWriter = new BufferedWriter(new FileWriter(file));
bWriter.write(stringBuffer.toString());
bWriter.close(); // 运行bat
Runtime r = Runtime.getRuntime();
Process child = r.exec("cmd.exe /c start " + path);
child.waitFor(); // 问题发现:这句没起作用,在上面BAT刚启命令行时候,下面就已经检查完10个文件了都是0行 // 使用完毕后删除文件
// new File("F:/1.bat").delete(); //同样的没有执行完毕批处理命令,文件都被删除了,所以也不能用这句。
} private static boolean readFileLineIs10(String path) throws IOException
{
int i = 0;
BufferedReader br = new BufferedReader(new FileReader(path));
String line = br.readLine();
while (line != null)
{
i++;
line = br.readLine();
} System.out.println(path + " 的行数为: " + i); return i == 10;
} // 有就删了创,没就创
private static File createFile(String path) throws IOException
{
File file = new File(path); if (file.exists())
{
file.delete();
} File parent = file.getParentFile(); if (parent != null && !parent.exists())
{
parent.mkdirs();
} file.createNewFile(); return file;
}}
问题确诊了,还是在那个waitfor发完命令都接到进行了,后面根本没给BAT执行完。但是算是好兆头,至少实现批处理写入不会漏写。不好的是,我不知道什么时间BAT文件执行完,因为这次虽然是只执行了一次exec,但是刚执行完就收到0了,所以在我尝试收到0删除时候直接命令行窗口出错停了