62,620
社区成员
发帖
与我相关
我的任务
分享
import java.io.IOException;
public class demo1
{
public static void main(String[] args) throws IOException, InterruptedException
{
String username = null;
String password = "admin";
for (int i = 1; i <= 10; i++)
{
username = "yulei" + i;
Runtime r = Runtime.getRuntime();
r.exec("cmd.exe /c start D:/svn/Apache2/bin/htpasswd.exe -b F:/passwd " + username + " " + password);
//Thread.sleep(3000); // 这里是我自己的本方法,休眠3秒,因为这个插入根本用不了3秒钟
}
}
}
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删除时候直接命令行窗口出错停了
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();
int value = child.exitValue();
System.out.println("子线程的返回值: " + value);
}
System.out.println(readFileLineIs10(path));
}
}
子线程的返回值: 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
谁来给指点一句!
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那边问了。