Java 是否正在处理保存到.txt文件?
我不知道如何在处理过程中保存到txt文件,我尝试了一些不同的方法,但每次似乎都会覆盖该文件,因为:Java 是否正在处理保存到.txt文件?,java,file,printing,output,processing,Java,File,Printing,Output,Processing,我不知道如何在处理过程中保存到txt文件,我尝试了一些不同的方法,但每次似乎都会覆盖该文件,因为: output = createWriter("positions.txt"); 如果我试图删除这一行,或者尝试其他方法来检查文件是否坚持,然后不运行这一行,我会发现NullPointerException 是否有任何方法可以在不覆盖文件的情况下保存文件 PrintWriter output; void setup() { // Create a new file in the sketch
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函数在每次调用时都会创建一个新文件 如果您在谷歌上搜索处理附加到文本文件或Java附加到文本文件,您将获得大量结果
基本上,您希望使用Java的PrintWriter类,它的构造函数采用布尔参数。如果该参数为true,则数据将附加到文件末尾,而不是覆盖原始文件。由于您使用的是相同的path positions.txt,因此文件将被覆盖。如果每次都要创建新文件,请使用while或for循环遍历数字,以检查positions+number+.txt是否存在。继续运行,直到文件不存在,并将其用作路径。@khelwood这是正在处理的,而不是Java-createWriter是处理的本机函数。考虑到这一点,op-Processing!=Java,因此值得删除Java标记。