2017-04-10 74 views
0

我不知道如何保存在处理一个txt文件,我已经尝试了一些不同的东西,但每次好像该文件的时间是因为重载:处理保存到.txt文件?

output = createWriter("positions.txt"); 

,如果我尝试删除此行,或尝试其他的东西来检查文件是否坚持,然后不运行该行,ia 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 
} 
+1

该文件被覆盖,因为您使用相同的路径'“positions.txt”'。如果您想每次创建一个新文件,请使用'while'或'for'循环遍历数字,以检查是否存在“+”位置“+ number +”.txt“。继续下去,直到文件不存在并将其用作路径。 – Nathangrad

+0

@khelwood这不是Java处理 - createWriter是Processing的本地函数。考虑到这一点,运算处理!= Java,所以值得移除Java标记。 –

回答

1

就像你已经注意到了,处理的createWriter()函数创建每次它被称为时间的新文件。

如果你谷歌“处理附加到文本文件”或“Java附加到文本文件”,你会得到很多结果。

基本上,您要使用Java的PrintWriter类,其构造函数采用boolean参数。如果该参数为true,则您的数据将被附加到文件的末尾而不是覆盖原始文件。

+0

非常感谢你的帮助:D – Lampen