Я пишу программу на Java, которая записывает несколько строк информации в файл CSV. Я хочу удалить последнюю строку CSV-файла, поскольку он не нужен. Как бы это сделать, поскольку файл CSV создается PrintWriter, и я не верю, что метод append мог бы это сделать.Как удалить строку в PrintWriter
Дополнительная строка написана потому, что цикл продолжается для одной дополнительной строки. Эта часть кода выглядит следующим образом:
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();
}
}
Вы можете не писать эту строку вместо этого? – kiruwka
Можете ли вы разместить код? Что вы пробовали? – rmlan
Вы правы. Писатели похожи на OutputStreams. Они просто добавляются каждый раз, когда вызывается их метод записи. Почему вы пишете строку в первую очередь, когда позже хотите удалить ее снова? – isnot2bad