2012-03-05 35 views
0

我有一些讀取文本文件的java代碼,將內容添加到一個矢量,然後使用圖形加密器將這些點打印到屏幕上。Java正在讀取舊的輸入文件,而不是新的文件

我有三個pointdata.txt文件,pointdata1.txt,pointdata2.txt和pointdata3.txt。

我遇到的問題是,即使當我將驅動程序中的輸入文件更改爲pointdata1或pointdata2時,它仍然會爲pointdata3運行。我確保代碼中沒有其他地方出現pointdata3。它只出現兩次,但我確定它是一樣的。 我已經檢查過這些文件,它們是不同的。檢查並檢查並檢查路徑名,它們是不同的!

即使我在整個代碼中註釋掉每個System.out.println(),它仍然會打印所有內容!

這是代碼不再引用文本文件,甚至運行,eclipse只是不斷打印以前添加到視口的內容?

這裏是我的司機代碼:

import java.util.*; 

public class PointDriver { 
private PointField pointfield; 

    // testing 
    public void doAllTests() throws Exception{ 
     this.test1(); 
     this.test2(); 
    } 

    // Display all points in the file 
    public void test1() throws Exception{ 

     SimpleIO sIO = new SimpleIO(); 

     System.out.println("Contents of Point File: "); 
     sIO.displayFile("pointdata1.txt"); 
     //sIO.displayFile("pointdata2.txt"); 
     //sIO.displayFile("pointdata3.txt");   
     System.out.println(); 

    } 

// Load points from a file into a vector and echo them back to the screen 
// This uses the StringTokenizer to split the lines into two Strings, then 
// uses the Point class to assign the two Strings to x,y double variables 
// which form Points. Within the same loop, the points are also displayed 
// in a window using the Graph Window class. Maximum x and y values are used 
// to determine the dimensions of the GraphWindow, adding 10 units to each 
// value to provide a border. 
    public void test2() throws Exception{ 

     System.out.println("Contents of Point File: "); 
     System.out.println(); 
     System.out.println("Points are Displayed in a Graph Window"); 
     System.out.println(); 

     Vector lines; 
     lines = pointfield.getlines("pointdata1.txt"); 
     //lines = pointfield.getlines("pointdata2.txt"); 
     //lines = pointfield.getlines("pointdata3.txt");    

     Iterator IT; 
     IT = lines.iterator();  

     Vector v; 
     v = new Vector(); 

     double maxX, maxY;   

     PointField pointfield; 
     pointfield = new PointField(); 

     GraphWindow gw; 
     gw = new GraphWindow(); 

     while (IT.hasNext()) { 

      StringTokenizer st; 

      String ID = (String)IT.next(); 
      st = new StringTokenizer(ID); 

      double x = Double.parseDouble(st.nextToken()); 
      double y = Double.parseDouble(st.nextToken()); 

      Point p; 
      p = new Point(x,y); 

      v.addElement(p); 

      int i = v.size(); 
      System.out.println("Point ID: " +i+ " X: "+x+", Y: "+y);  

      gw.plotPoint(x, y);     
     } 

     this.pointfield = new PointField(v); 
     maxX = this.pointfield.findMaxXPoint(); 
     maxY = this.pointfield.findMaxYPoint(); 

     int width = (int)maxX + 10; 
     int height = (int)maxY + 10; 

     gw.setMap(width, height);  
    } 

    // Short main method to kick of all tests sequence in doAllTests method 
    public static void main(String[] args) throws Exception { 
     PointFieldDriver pfd; 
     pfd = new PointFieldDriver(); 
     pfd.doAllTests(); 
    } 
} 
+1

我們必須看到您的代碼才能確切地知道。我建議你運行一個調試器。另外,你的項目編譯?您可以嘗試構建JAR文件並查看運行該文件會發生什麼情況。 – 2012-03-05 01:42:57

+0

會從我的驅動程序添加代碼會有幫助嗎? – alice 2012-03-05 01:43:40

+0

您可以給我們提供的所有相關內容都很有幫助。 – 2012-03-05 01:44:38

回答

1

它看起來像Eclipse運行的是舊版本的類文件。我正在收集這些信息,因爲你說你已經註釋掉了printlns,但輸出仍然顯示。

有幾件事情要檢查:

  • 在菜單中,確保項目>自動構建設置。如果沒有設置,設置它,並且你的問題應該被解決。
  • 查看更改源時,類文件上的時間戳是否更改。如果不是,那麼Eclipse不會因爲某種原因重新編譯它。您可以在bin文件夾下找到類文件(如果您使用的是Eclipse項目的默認值)。
  • 如果您發現時間戳較舊,請嘗試從Eclipse外部移除bin文件夾。接下來,右鍵單擊該項目並選擇刷新。最後,選擇項目>清潔。這應該導致代碼被重新編譯到一個新的類文件中。
+0

是的,您的問題將是Eclipse的8倍。升級到IDEA,讓您的生活更輕鬆。 – Synesso 2012-03-05 02:03:06

+0

好吧..所以我試着你說的一切,它運行舊的.CLASS文件,所以我清理垃圾箱 - 現在它每次運行代碼時都會產生新的垃圾..但它仍在運行舊代碼!這怎麼可能? – alice 2012-03-05 02:03:25

+0

如果我嘗試打開它在記事本中創建的.CLASS文件,那麼它們充滿了展翅 - 或者它們是這樣的嗎? – alice 2012-03-05 02:04:53