2012-05-08 49 views
0

我有一個java應用程序,它從webpanel獲取命令,當它執行命令時,它會保存在某個地方,以便它知道它已經執行。那麼當它接下來執行一個命令時,它在執行該命令之前檢查該列表,這對PC來說工作正常,但在Mac上,它似乎不起作用。它保存命令,但是當它檢查新命令時,它也會執行所有以前的命令。 n3.data包含:Java webpanel命令不保存

1,2,3,4,


每個命令被賦予的ID(在這種情況下1 2 3和4),該應用應該檢查它使用了什麼命令ID,然後執行,如果該ID不在指定文件中(n3.data)
這裏是代碼。

public void save(int id) { 
    String osName = System.getProperty("os.name"); 
    if(osName.contains("Windows")){ 
    try { 
     BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.home") + "\\app.data", true)); 
     bw.write(id + ","); 
     bw.close(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
    } else if(osName.contains("Mac")){ 
     try { 
      BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.home") + "/n3.data", true)); 
      bw.write(id + ","); 
      bw.close(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

public void createNew() { 
    String osName = System.getProperty("os.name"); 
    File win = new File(System.getProperty("user.home") + "\\app.data"); 
    File mac = new File(System.getProperty("user.home") + "/n3.data"); 
    if(osName.contains("Windows") && !win.exists()){ 
    try { 
     new File(System.getProperty("user.home") + "\\app.data").createNewFile(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
    }else if(osName.contains("Mac") && !mac.exists()){ 
     try { 
      new File(System.getProperty("user.home") + "/n3.data").createNewFile(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

public void saveNew() { 
    String osName = System.getProperty("os.name"); 
    if(osName.contains("Windows")){ 
    StringBuilder sb = new StringBuilder(); 
    for (int i : processedIds) { 
     sb.append(i + ","); 
    } 
    try { 
     BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.home") + "\\app.data")); 
     bw.write(sb.toString()); 
     bw.close(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
    }else if(osName.contains("Mac")){ 
     StringBuilder sb = new StringBuilder(); 
     for (int i : processedIds) { 
      sb.append(i + ","); 
     } 
     try { 
      BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.home") + "/n3.data")); 
      bw.write(sb.toString()); 
      bw.close(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
    } 
    } 
} 

public void loadSave() throws IOException { 
    String osName = System.getProperty("os.name"); 
    if(osName.contains("Windows")){ 
     File file = new File(System.getProperty("user.home") + "\\app.data"); 
     if (file.exists()) { 
      BufferedReader br = new BufferedReader(new FileReader(file)); 
      StringBuilder sb = new StringBuilder(); 
      String line; 
      while ((line = br.readLine()) != null) { 
       sb.append(line); 
      } 
      br.close(); 
      for (String s : sb.toString().split(",")) { 
       try { 
        processedIds.add(Integer.parseInt(s)); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } else { 
      file.createNewFile(); 
     } 
    }else if(osName.contains("Mac")){ 
     File file = new File(System.getProperty("user.home") + "/n3.data"); 
     if (file.exists()) { 
      BufferedReader br = new BufferedReader(new FileReader(file)); 
      StringBuilder sb = new StringBuilder(); 
      String line; 
      while ((line = br.readLine()) != null) { 
       sb.append(line); 
      } 
      br.close(); 
      for (String s : sb.toString().split(",")) { 
       try { 
        processedIds.add(Integer.parseInt(s)); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } else if (!file.exists()){ 
      file.createNewFile(); 
     } 
    } 
} 

回答

0

我想通了,有同樣的問題的人,該代碼不正確,如果該文件存在與否進行檢查。只是解決這個問題,它應該工作。