Java 如何在PrintWriter中删除行

Java 如何在PrintWriter中删除行,java,printwriter,writer,Java,Printwriter,Writer,我正在用Java编写一个程序,将几行信息写入CSV文件。我想删除CSV文件的最后一行,因为它不是必需的。我该怎么做,因为CSV文件是由PrintWriter创建的,我不相信append方法可以做到这一点 额外的行被写入,因为循环将继续一个额外的行。这部分代码如下所示: public static void obtainInformation() throws IOException { PrintWriter docketFile = new PrintWriter(new FileW

我正在用Java编写一个程序,将几行信息写入CSV文件。我想删除CSV文件的最后一行,因为它不是必需的。我该怎么做,因为CSV文件是由PrintWriter创建的,我不相信append方法可以做到这一点

额外的行被写入,因为循环将继续一个额外的行。这部分代码如下所示:

public static void obtainInformation() throws IOException {

    PrintWriter docketFile = new PrintWriter(new FileWriter("ForclosureCourtDockets"+startingMonth+"_"+startingDay+"_"+startingYear+"-"+endingMonth+"_"+endingDay+"_"+endingYear+".csv", true)); 
    pageContentString = pageContentString.replace('"','*');
    int i = 0;
    boolean nextDocket = true;

    while(nextDocket) {
      nextDocket = false;

      String propertyCity = "PropertyCity_"+i+"*>"; 
      Pattern propertyCityPattern = Pattern.compile("(?<="+Pattern.quote(propertyCity)+").*?(?=</span>)");
      Matcher propertyCityMatcher = propertyCityPattern.matcher(pageContentString); 

      while (propertyCityMatcher.find()) {
        docketFile.write(i+propertyCityMatcher.group().toString()+", "); 
        nextDocket = true;
      }

      String descriptionValue = "Description_"+i+"*>"; 
      Pattern descriptionPattern = Pattern.compile("(?<="+Pattern.quote(descriptionValue)+").*?(?=</span>)");
      Matcher descriptionMatcher = descriptionPattern.matcher(pageContentString); 

      while (descriptionMatcher.find()) {
        docketFile.write(descriptionMatcher.group().toString()+"\n"); 
      }

      i++;
    }

    docketFile.close();

  }

 public static void removeLineFromFile() {

try {

  File inFile = new File("ForclosureCourtDockets"+startingMonth+"_"+startingDay+"_"+startingYear+"-"+endingMonth+"_"+endingDay+"_"+endingYear+".csv");

  if (!inFile.isFile()) {
    System.out.println("Parameter is not an existing file");
    return;
  }

  //Construct the new file that will later be renamed to the original filename.
  File tempFile = new File(inFile.getAbsolutePath() + ".tmp");

  BufferedReader br = new BufferedReader(new FileReader("ForclosureCourtDockets"+startingMonth+"_"+startingDay+"_"+startingYear+"-"+endingMonth+"_"+endingDay+"_"+endingYear+".csv"));
  PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

  String line = null;

  //Read from the original file and write to the new
  //unless content matches data to be removed.
  while ((line = br.readLine()) != null) {

    if (!line.trim().equals("^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^")) {

      pw.println(line);
      pw.flush();
    }
  }
  pw.close();
  br.close();

  //Delete the original file
  if (!inFile.delete()) {
    System.out.println("Could not delete file");
    return;
  }

  //Rename the new file to the filename the original file had.
  if (!tempFile.renameTo(inFile))
    System.out.println("Could not rename file");

}
catch (FileNotFoundException ex) {
  ex.printStackTrace();
}
catch (IOException ex) {
  ex.printStackTrace();
}
 }
public static void obtainInformation()引发IOException{
PrintWriter docketFile=新的PrintWriter(新文件编写器(“ForclosureCourtDockets”+起始月+““+起始日+““+起始年+“-”+结束月+““+结束日+““+结束年+”.csv”,true));
pageContentString=pageContentString.replace(“,”*);
int i=0;
布尔值nextDocket=true;
while(nextDocket){
nextDocket=假;
字符串propertyCity=“propertyCity_quo+i+“*>”;
模式属性citypattern=Pattern.compile(“(?”;

Pattern description Pattern=Pattern.compile((?很好,因为
PrintWriter
只是一个
编写器
,它只能
追加
/
写入
/
打印

因此,您不能覆盖刚刚写入的行。
您有几个选择:

  • 修改您的逻辑,以确保您不会写入最终要删除的行(我认为最符合逻辑的选项)
  • 写入文件后,您可以使用另一个
    读取器
    (例如,
    BufferedReader
    )再次读取该文件,然后重新写入,而不需要排除该行
  • 使用及其
    seek
    方法返回并重写/删除所需的行

您可以执行以下操作(这是实现kiruwka第一个建议的一种方法):

1) 在while循环外写入第一行

2) 然后,对于while循环中写入的每一行,首先输出换行符,然后仅输出csv行


这样,您将拥有重复的代码,但不会在文件末尾出现换行符。您还可以将重复的代码分解成一个帮助器方法。

您可能不会改为写那一行吗?您可以发布一些代码吗?您尝试了什么?您是对的。编写器就像输出流一样。每次写入方法时,它们都会追加ods被调用。当您以后要再次删除它时,为什么要在第一个位置写入该行?请查看