我最近做了一個爲網站做備份的應用程序。有一部分爲網站進行備份,一部分爲數據庫進行備份。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的文本文件,以示區別。