J'ai un fichier texte que j'utilise pour stocker des informations sur les éléments.Comment supprimer une ligne vide à la fin du fichier?
10325,Test1,Producto del Hogar,12,17
10425,Test2,Videojuegos,11,17
12345,Test3,Producto del Hogar,56,17.0
J'utilise la bibliothèque opencsv pour modifier une colonne de l'élément souhaité, je suis en utilisant ce code:
public void updateCSV(String replace, int fila, int col) throws IOException {
if (replace != null) {
File archivoProductos = new File("productos.txt");
// Read existing file
CSVReader reader = new CSVReader(new FileReader(archivoProductos), ',', CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.NO_ESCAPE_CHARACTER);
List<String[]> csvBody = reader.readAll();
// get CSV row column and replace with by using row and column
csvBody.get(fila)[col] = replace;
reader.close();
// Write to CSV file which is open
CSVWriter writer = new CSVWriter(new FileWriter(archivoProductos), ',', CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.NO_ESCAPE_CHARACTER, System.getProperty("line.separator"));
writer.writeAll(csvBody);
writer.flush();
writer.close();
}
}
Le problème est que lorsque la modification est faite, on ajoute un ligne vide à la fin du fichier texte.
line1 10325,Test99,Producto del Hogar,12,17
line3 10425,Test2,Videojuegos,11,17
line2 12345,Test3,Producto del Hogar,56,17.0
line4
Comment puis-je éviter la ligne vide ajoutée à la fin?
Ajouté println de csvBody avant qu'il l'écrit sur
[10325, Test99, Producto del Hogar, 12, 17]
[10425, Test2, Videojuegos, 11, 17]
[12345, Test3, Producto del Hogar, 56, 17.0]
Essayé jusqu'à présent:
Ne pas ajouter la ligne vide, mais mes 3 lignes existantes sont maintenant écrites en un ligne.
CSVWriter writer = new CSVWriter(new FileWriter(archivoProductos), ',',
CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.NO_ESCAPE_CHARACTER,"");
À quoi ressemble csvBody avant de l'écrire? vous pouvez utiliser des instructions d'impression pour voir. –
Pouvez-vous essayer avec ça? CSVWriter writer = nouveau CSVWriter (nouveau FileWriter (archivoProductos), ',', CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER); – developer
J'ai mis à jour le post, comme vous pouvez le voir il n'y a pas de ligne vide dans le csvBody, Si je supprime la ligne vide et que je veux modifier une autre ligne, la ligne vide sera de retour. @melgart –