我最近做了一个为网站做备份的应用程序。有一部分为网站进行备份,一部分为数据库进行备份。while loop background worker broken
当我用硬编码数据运行应用程序后,我决定使用.txt文件来读取值,因此不需要更改java应用程序中的数据。这样,每次添加网站时都不必重新编译应用程序。
当我添加该.txt reade我的while循环停止工作,我不知道为什么。也许我犯了一个基本的错误,但我没有看到什么。我希望你能帮忙。
我包括了IF函数,因为文本文件的读取读取两次的东西,这样就不会尝试打开datbases名为根:
if (!"root".equals(dbName)) {
executeCmd = init + command;
String printDate = new SimpleDateFormat(" yyyy-MM-dd - HH mm ss").format(Calendar.getInstance().getTime());
JTextArea.append("\n" + printDate + executeCmd);
/*NOTE: Executing the command here*/
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
processComplete = runtimeProcess.waitFor();
}
我想这可能是这个,但是当我关闭它没有任何变化。
我现在有这样的代码(我禁用了IP地址等)
public class executeCmd1 {
public String dbName;
public String dbUser;
public String part1;
public String part2;
public String executeCmd;
public int processComplete;
public void executeCmd1() {
worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
while (true) {
System.out.println("Tekst1");
try {
System.out.println("Tekst2");
System.out.println("Reading File from Java code");
//Name of the file
//*NOTE: Getting path to the Jar file being executed*/
//*NOTE: YourImplementingClass-> replace with the class executing the code*/
CodeSource codeSource = executeCmd1.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
String jarDir = jarFile.getParentFile().getPath();
String fileName = "Textfile\\textfile.txt";
//Create object of FileReader
FileReader inputFile = new FileReader(fileName);
//Instantiate the BufferedReader Class
BufferedReader bufferReader = new BufferedReader(inputFile);
//Variable to hold the one line data
String line;
// Read file line by line and print on the console
line = bufferReader.readLine();
String[] strs = line.split("-");
System.out.println("Substrings length:" + strs.length);
for (int i = 0; i < strs.length; i++) {
String onderdelen = (strs[i] + "-" + strs[(i + 1)]);
String[] parts = onderdelen.split(Pattern.quote("-"));
part1 = parts[0];
part2 = parts[1];
System.out.println(part1 + " " + part2);
//Close the buffer reader
bufferReader.close();
/*NOTE: Creating Database Constraints*/
dbName = part1;
dbUser = part2;
/*NOTE: Creating Path Constraints for folder saving*/
//*NOTE: Here the backup folder is created for saving inside it*/
String folderPath = jarDir + "\\backup";
/*NOTE: Creating Folder if it does not exist*/
File f1 = new File(folderPath);
f1.mkdir();
/*NOTE: Creating Path Constraints for backup saving*/
//*NOTE: Here the backup is saved in a folder called backup with the name backup.sql*/
String init = "cmd /c start timeout 0 & cd /d C:\\xampp\\mysql\\bin\\ & ";
String checkoutDate = new SimpleDateFormat(" yyyy-MM-dd - HH mm ss").format(Calendar.getInstance().getTime());
String command = "mysqldump -P 3306 -h 192.168.50.166 -u " + dbUser + " --databases " + dbName + " -r \"%cd%\\backup\\backup " + checkoutDate + dbName + " file.sql\" & start cmd /c echo fisished ^& timeout 5";
JTextArea.append("\n Er wordt een backup gemaakt van " + dbName + " en op de gebruiker " + dbUser);
/*NOTE: Used to create a cmd command*/
if (!"root".equals(dbName)) {
executeCmd = init + command;
String printDate = new SimpleDateFormat(" yyyy-MM-dd - HH mm ss").format(Calendar.getInstance().getTime());
JTextArea.append("\n" + printDate + executeCmd);
/*NOTE: Executing the command here*/
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
processComplete = runtimeProcess.waitFor();
}
/*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
if (processComplete == 0) {
String printDate = new SimpleDateFormat(" yyyy-MM-dd - HH mm ss").format(Calendar.getInstance().getTime());
JTextArea.append("\n" + printDate + " Backup van datbase compleet");
} else {
String printDate = new SimpleDateFormat(" yyyy-MM-dd - HH mm ss").format(Calendar.getInstance().getTime());
JTextArea.append("\n" + printDate + " Backup van database mislukt");
}
Thread.sleep(4000);
}
} catch (URISyntaxException | IOException | InterruptedException ex) {
return null;
}
System.out.println("Tekst3");
}
}
};
worker.execute();
System.out.println("Tekst4");
}
}
如果有必要,我可以提供的代码之前,我uncluded的文本文件,以示区别。