我最近制作了一个为网站备份的应用程序。有一部分用于备份网站,另一部分用于数据库。
我使用硬编码数据运行应用程序后,我认为最好使用.txt文件读取值,这样就不需要更改java应用程序中的数据了。这样您就不必在每次添加网站时重新编译应用程序。
我添加了.txt reade我的while循环停止工作,我不知道为什么。也许我犯了一个基本错误,但我看不出是什么。我希望你能提供帮助。
我包含了一个if函数,因为读取文本文件两次读取内容所以它不会尝试打开名为root的数据库:
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");
}
}
如果有必要,我可以在删除文本文件之前提供代码以显示差异。
答案 0 :(得分:0)
for循环中存在一个问题,它从.txt文件中读取值。
它正在搜索不存在的值,因此它在for循环中崩溃。
该循环的修正如下所示。
for (int i = 0; i < (strs.length - 1); i++) {
System.out.println("start of for loop");
String onderdelen = (strs[i] + "-" + strs[(i + 1)]);
答案 1 :(得分:0)
你提供了非常详细的代码,也许你可以稍微删除你的例子。
我认为问题在于你打开文件,读取一行,拆分行,并为每个子串关闭BufferedReader。你应该只关闭一次BufferedReader。
我的建议是使用java.util.Properties来读取文件(Properties.load(Reader))。然后,您可以使用getProperty()读取文件中的值。