2016-09-20 2 views
0

최근에 웹 사이트에 대한 백업을 만드는 응용 프로그램을 만들었습니다. 웹 사이트에 대한 백업과 데이터베이스에 대한 백업을 만드는 부분이 있습니다.루프 배경 작업자가 깨진 경우

하드 코딩 된 데이터로 응용 프로그램을 실행 한 후에는 .txt 파일을 사용하여 값을 읽을 때 Java 응용 프로그램의 데이터를 변경할 필요가 없다고 결정한 것이 좋습니다. 이렇게하면 웹 사이트를 추가 할 때마다 응용 프로그램을 다시 컴파일하지 않아도됩니다.

내가 .txt reade를 추가 한 후 while while loop가 작동을 멈췄으며 이유를 알 수 없었다. 어쩌면 나는 기본적인 실수를했을지 모르지만 나는 무엇을 보지 못한다. 네가 도울 수 있기를 바란다. 내가 사용하지 않는 경우 내가 이렇게 될 줄 알았는데

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(); 
} 

하지만 : 그것은 시도하고 이름 root 오픈 datbases되지 않도록 기능은 텍스트 파일의 읽기 두 번 물건을 읽기 때문에 경우

는 내가 포함 아무것도 바뀌지 않습니다. 나는 차이를 보여 텍스트 파일을 uncluded 전에 코드를 제공 할 수 있습니다 필요한 경우

내가 현재이 코드가

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"); 
    } 
} 

(I는 IP ADRESS 등을 사용할 수 없습니다).

답변

0

.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)]); 
0

매우 자세한 코드를 제공 했으므로 예제를 약간 삭제할 수 있습니다.

나는 파일을 열고, 한 줄을 읽고, 줄을 나누고, 각 하위 문자열에 대해 BufferedReader를 닫는 것이 문제라고 생각합니다. BufferedReader는 한 번만 닫아야합니다.

내 제안은 java.util.Properties를 사용하여 파일 (Properties.load (Reader))을 읽는 것입니다. 그런 다음 getProperty()로 파일의 값을 읽을 수 있습니다.

관련 문제