java 如何在 Processing 中将文本附加到 csv/txt 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17010222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:42:18  来源:igfitidea点击:

How do I append text to a csv/txt file in Processing?

javacsvprocessing

提问by sfewfed

I use this simple code to write a few strings to the file called "example.csv", but each time I run the program, it overwrites the existing data in the file. Is there any way to append the text to it?

我使用这个简单的代码将几个字符串写入名为“example.csv”的文件,但每次运行该程序时,它都会覆盖文件中的现有数据。有没有办法将文本附加到它?

void setup(){
  PrintWriter output = createWriter ("example.csv");
  output.println("a;b;c;this;that ");
  output.flush();
  output.close();

}

回答by Pwdr

import java.io.BufferedWriter;
import java.io.FileWriter;

String outFilename = "out.txt";

void setup(){
  // Write some text to the file
  for(int i=0; i<10; i++){
    appendTextToFile(outFilename, "Text " + i);
  } 
}

/**
 * Appends text to the end of a text file located in the data directory, 
 * creates the file if it does not exist.
 * Can be used for big files with lots of rows, 
 * existing lines will not be rewritten
 */
void appendTextToFile(String filename, String text){
  File f = new File(dataPath(filename));
  if(!f.exists()){
    createFile(f);
  }
  try {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f, true)));
    out.println(text);
    out.close();
  }catch (IOException e){
      e.printStackTrace();
  }
}

/**
 * Creates a new file including all subfolders
 */
void createFile(File f){
  File parentDir = f.getParentFile();
  try{
    parentDir.mkdirs(); 
    f.createNewFile();
  }catch(Exception e){
    e.printStackTrace();
  }
}    

回答by jesses.co.tt

You have to use a FileWriter (pure Java (6 or 7)) rather than PrintWriter from the Processing API. FileWriter has a second argument in it's constructor that allows you to set a Boolean to decide whether you will append the output or overwrite it (true is to append, false is to overwrite).

您必须使用来自 Processing API 的 FileWriter(纯 Java(6 或 7))而不是 PrintWriter。FileWriter 在它的构造函数中有第二个参数,它允许您设置一个布尔值来决定是附加输出还是覆盖它(true 是附加,false 是覆盖)。

The documentation is here: http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.htmlNote you can also use a BufferedWriter, and pass it a FileWriter in the constructor if that helps at all (but I dont think it's necessary in your case).

文档在这里:http: //docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html请注意,您还可以使用 BufferedWriter,并在构造函数中将它传递给 FileWriter,如果这有帮助的话全部(但我认为在您的情况下没有必要)。

Example:

例子:

try {
  FileWriter output = new FileWriter("example.csv",true); //the true will append the new data
  output.println("a;b;c;this;that ");
  output.flush();
  output.close();
}
catch(IOException e) {
  println("It Broke :/");
  e.printStackTrace();
}

As above, this will work in the PDE - and in Android - but if you needto use it in PJS, PyProcessing, etc, then you will have to hack it

如上所述,这将适用于 PDE - 和 Android - 但如果您需要在 PJS、PyProcessing 等中使用它,那么您将不得不破解它

  • dynamically read the length of the existing file and store it in an ArrayList
  • add a new line to the ArrayList
  • use the ArrayList index to control where in the file you are currently writing
  • 动态读取现有文件的长度并将其存储在一个ArrayList中
  • 向 ArrayList 添加新行
  • 使用 ArrayList 索引来控制您当前正在写入的文件中的位置

If you want to suggest an enhancement to the PrintWriter API (which is probably based off of FileWriter), you can do so at Processing's Issue page on GitHub:

如果您想对 PrintWriter API(可能基于 FileWriter)提出改进建议,您可以在 GitHub 上的处理问题页面上这样做:

https://github.com/processing/processing/issues?state=open

https://github.com/processing/processing/issues?state=open

回答by Mike 'Pomax' Kamermans

Read in the file's data, append your new data to that, and write the appended data back to the file. Sadly, Processing has no true "append" mode for file writing.

读入文件的数据,将您的新数据附加到该文件中,并将附加的数据写回文件。遗憾的是,Processing 没有真正的“追加”文件写入模式。