我正在做一个小应用程序,我在一些圆圈、正方形和三角形周围移动。我从 txt 文件中读取的坐标。但是一旦我完成移动它们,我想将它们的坐标保存在同一个 txt 文件中。
这就是代码现在的样子:
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
没有任何好结果。