I'm making a program that fixes a separator in csv file, and my idea was read all the lines, then check each line for the wrong separator, then replace it with the good one. I've been trying multiple things, but this is where I'm stuck right now. This is the code:
try (FileWriter escritor = new FileWriter(archivoRes.getAbsoluteFile()); BufferedReader lectorArchivo = new BufferedReader(new FileReader(archivoSelec)); BufferedWriter bfw = new BufferedWriter(escritor)) {
byte[] byteArray = new byte[(int) archivoSelec.length()];
FileInputStream fis = new FileInputStream(archivoSelec);
fis.read(byteArray);
int count = 1;
for (int i = byteArray.length; i >= 0; i--) {
String line = lectorArchivo.readLine();
if (line.indexOf(":") == 82 || line.indexOf(":") == 68 || line.indexOf(":") == 81) {
line.replaceFirst(":", ";");
}
bfw.write(line);
bfw.newLine();
}
bfw.close();
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
Any ideas on how I could get this through?
UPDATE:
I've discovered StringTokenizer, which seems easier, but I can't still get it working. This is the code at the moment.
for(int i = byteArray.length;i >= 0;i--){
String line = lectorArchivo.readLine();
StringTokenizer token = new StringTokenizer(line, ";");
if (token.countTokens() == 3) {
line.replaceAll(separador_old, separador);
}
bfw.write(line);
bfw.newLine();
}
bfw.close();