2012-12-07 156 views
-1

我正在做一個小應用程序,我正在繞着一些圓圈,正方形和三角形移動。我從txt文件中讀取哪個座標。但一旦我完成移動它們,我想將它們的座標保存在同一個txt文件中。將字符串保存到Java中的文本文件中

這是怎樣的代碼看起來像現在:

import java.io.FileNotFoundException; 
import se.lth.cs.ptdc.window.SimpleWindow; 

public class ShapeTest { 

    public static void main(String[] args) throws FileNotFoundException { 
      SimpleWindow w = new SimpleWindow(600, 600, "ShapeTest"); 
      ShapeList shapes = new ShapeList(); 
      java.util.Scanner scan = null; 
      try { 
       scan = new java.util.Scanner(new java.io.File("shapedata.txt")); 
      } catch (java.io.FileNotFoundException e) { 
       System.err.println("shapedata.txt couldn't be found"); 
      } 

      int x,y,z; 
      while(scan.hasNext()) { 
        String s = scan.next(); 
        if (s.contentEquals("S")){ 
          x = scan.nextInt(); 
          y = scan.nextInt(); 
          z = scan.nextInt(); 
          shapes.insert(new Square(x,y,z)); 
        } else if (s.contentEquals("C")) { 
          x = scan.nextInt(); 
          y = scan.nextInt(); 
          z = scan.nextInt(); 
          shapes.insert(new Circle(x,y,z)); 
        } else if (s.contentEquals("T")) { 
          x = scan.nextInt(); 
          y = scan.nextInt(); 
          z = scan.nextInt(); 
          shapes.insert(new Triangle(x,y,z)); 
        } 

      } 
      shapes.draw(w); 

      CommandDispatcher cd = new CommandDispatcher(w,shapes); 
      cd.mainLoop(); 
    } 
} 

我需要什麼補充?我試過FileUtils.writeStringToFile沒有任何好的結果。

+1

什麼是'FileUtils.writeStringToFile'問題了嗎? – rekire

+0

'JTextComponent'中'SimpleWindow'中的'String'是否可訪問? –

+0

Rekire,不知道,但我沒有得到它正常工作。也許我把它放在一個錯誤的地方。 – Michael

回答

0

您可以嘗試使用java.util.Formatter驗證您的文本文件,然後格式化輸出,並將它們存儲在同一個文本文件中。

相關問題