while循环后台工作者坏了

While loop background worker broken

我最近制作了一个为网站备份的应用程序。一部分备份网站,一部分备份数据库。

在我获得应用程序 运行 硬编码数据后,我决定最好使用 .txt 文件从中读取值,因此无需更改 [=26= 中的数据] 应用。这样您就不必在每次添加网站时都重新编译应用程序。

在我添加 .txt 读取后,我的 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");
    }
}

如有必要,我可以在取消包含文本文件之前提供代码以显示差异。

从 .txt 文件读取值的 for 循环出现问题。

它正在搜索不存在的值,因此它在 for 循环中崩溃了。

那个循环的修复看起来像这样。

for (int i = 0; i < (strs.length - 1); i++) {
System.out.println("start of for loop");
String onderdelen = (strs[i] + "-" + strs[(i + 1)]);

您提供了非常详细的代码,也许您可​​以精简一下您的示例。

我认为问题在于您打开文件、读取一行、拆分该行并为每个子字符串关闭 BufferedReader。您应该只关闭 BufferedReader 一次。

我的建议是使用java.util.Properties读取文件(Properties.load(Reader))。然后您可以使用 getProperty().

读取文件中的值