用Java将字符串保存到文本文件

用Java将字符串保存到文本文件,java,string,text,file-io,Java,String,Text,File Io,我在做一个小应用程序,在其中我移动一些圆,正方形和三角形。我从txt文件中读取的坐标。但是一旦我移动完它们,我想把它们的坐标保存在同一个txt文件中 下面是代码现在的样子: import java.io.FileNotFoundException; import se.lth.cs.ptdc.window.SimpleWindow; public class ShapeTest { public static void main(String[] args) throws FileN

我在做一个小应用程序,在其中我移动一些圆,正方形和三角形。我从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
,但没有任何好结果。

您可以尝试使用java.util.Formatter验证您的文本文件,然后格式化输出,将其存储在同一文本文件中。

如果您在保存int时遇到问题,只需使用
String.valueOf()
将值转换为字符串并正常保存。然后可以使用
Integer.parseInt()
读回它们


如果你问的是如何保存到一个文件,这是一个更大的问题,而且
FileUtils.writeStringToFile
有什么问题?是
SimpleWindow
中的
字符串
在一个
JTextComponent
中,是可以访问的吗。也许我把它放错地方了。它不工作的时候你把它放在哪里了?我在你的代码中没有看到(
FileUtile.writeStringToFile
)创建文件输出流并将坐标写入文件有什么问题?