Je fais une petite application où je déplace des cercles, des carrés et des triangles. Quelles coordonnées j'ai lues à partir d'un fichier txt. Mais une fois que j'ai fini de les déplacer, je voudrais enregistrer leurs coordonnées dans le même fichier txt.Enregistrer une chaîne dans un fichier texte en Java
Voici comment le code ressemble en ce moment:
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();
}
}
Qu'aurais-je besoin d'ajouter? J'ai essayé FileUtils.writeStringToFile
sans aucun bon résultat.
Quel est votre problème avec 'FileUtils.writeStringToFile'? – rekire
Est-ce que le 'String' dans' SimpleWindow' dans un 'JTextComponent' est accessible? –
Rekire, aucune idée mais je ne l'ai pas eu pour fonctionner correctement. Peut-être que je l'ai mis au mauvais endroit. – Michael