2016-02-19 79 views
0

我正在運行Java程序來計算樹中的所有路徑。當我在窗口中運行eclipse時,我看到了output1,但是當我從jar或Mac運行相同的程序時,我注意到了不同的輸出。有很多差異。即使文件大小也不同。依賴於平臺時,緩衝區寫入器的行爲會有所不同嗎當從Windows執行Java程序時會產生不同的輸出結果

所以,我從jar中執行或在Mac Eclipse上執行時有相同的輸出,但在從eclipse執行時會有不同的輸出。

下面是代碼多數民衆贊成寫入文件: 成員變量:

 
    public HashMap&ltString, HashSet&ltString>> nodeListFromFile = new HashMap&ltString, HashSet&ltString>>(); 

Funciton: 
    public void getAllPaths(String root, String path){ 
     //Since we are assuming there are approximately 12-16 levels 
     //And we are expecting a LCA in less than 16 steps 
     //All paths evaluated are of max size of 16 using this counter 
     int stepCounter = 0; 
     File file = new File(outPutfilePath); 

     try{ 
      if(!file.exists()){ 
       file.createNewFile(); 
      } 


      FileWriter fw = new FileWriter(file,true); 
      BufferedWriter bw = new BufferedWriter(fw); 

      //Iterate over each child node 
      for(String childNode: nodeListFromFile.get(root)){ 
       String tempPath = path; 
       if((tempPath.indexOf(childNode) grandChildSet = nodeListFromFile.get(childNode); 
        boolean goodGrandChild = true; 
        for(String gc: grandChildSet){ 
         if(!path.contains(gc)) 
          goodGrandChild = false; 
        } 

        if(grandChildSet.size()==0 || goodGrandChild){ 
         bw.write(tempPath+System.getProperty("line.separator")); 
         bw.flush(); 
        } 
       } 
      } 
      //End iteration of children 
     } 
     catch (Exception e) { 
      // TODO: handle exception 
     } 
     finally{ 
     } 

    }//End of function 
+0

編輯成員變量: \t public HashMap > nodeListFromFile = new HashMap >(); – rohan

+0

你可以點擊'編輯'來編輯你的問題。 – Bruno

+2

'Mac和Windows之間'System.getProperty(「line.separator」)'不同 – Ferrybig

回答

0

您使用line.separator,這是取決於系統

  • 這是Windows的\r\n
  • 對於Mac,它是\r

而且(比特有關),

  • 這是\n的OSX和Linux

所以,你會得到不同的輸出。

+0

我認爲它不僅僅是這樣。我正在處理一棵擁有近600,000個獨特節點的樹。從Mac生成的輸出是一些KB,而從窗口輸出的是GB。差異是顯着的。 – rohan

相關問題