处理保存到 .txt 文件?
Processing saving to .txt file?
我不知道如何在处理过程中保存到 txt 文件,我尝试了一些不同的东西,但每次似乎文件都被覆盖了,因为:
output = createWriter("positions.txt");
如果我尝试删除此行,或尝试其他方法来检查文件是否坚持然后不 运行 该行,我将出现 NullPointerException。
有没有什么方法可以在不覆盖文件的情况下保存文件?
PrintWriter output;
void setup() {
// Create a new file in the sketch directory
output = createWriter("positions.txt");
}
void draw() {
point(mouseX, mouseY);
output.println(mouseX); // Write the coordinate to the file
}
void keyPressed() {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
如您所见,Processing 的 createWriter()
函数在每次调用时都会创建一个新文件。
如果你 google "Processing append to text file" 或 "Java append to text file" 你会得到很多结果。
基本上,您想使用 Java 的 PrintWriter
class,其构造函数采用 boolean
参数。如果该参数是 true
,您的数据将附加到文件末尾,而不是覆盖原始数据。
我不知道如何在处理过程中保存到 txt 文件,我尝试了一些不同的东西,但每次似乎文件都被覆盖了,因为:
output = createWriter("positions.txt");
如果我尝试删除此行,或尝试其他方法来检查文件是否坚持然后不 运行 该行,我将出现 NullPointerException。
有没有什么方法可以在不覆盖文件的情况下保存文件?
PrintWriter output;
void setup() {
// Create a new file in the sketch directory
output = createWriter("positions.txt");
}
void draw() {
point(mouseX, mouseY);
output.println(mouseX); // Write the coordinate to the file
}
void keyPressed() {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
如您所见,Processing 的 createWriter()
函数在每次调用时都会创建一个新文件。
如果你 google "Processing append to text file" 或 "Java append to text file" 你会得到很多结果。
基本上,您想使用 Java 的 PrintWriter
class,其构造函数采用 boolean
参数。如果该参数是 true
,您的数据将附加到文件末尾,而不是覆盖原始数据。