I have the following program that reads a text file, changes the format of a specified date (Issue Date) and then writes everything back to a different file with the correct date format.
The issue is that when it rewrites the file it does it in UTF-8 and I need it to rewrite it in ISO_8859-1
How can I do this ?
public class DateFormater {
public static void main(String[] args) throws IOException {
if(args.length == 0){
System.out.println("Faltan argumentos");
return;
}
try {
System.out.println("Leyendo Archivos de Configuración ... ");
Configuration conf = new Configuration("../resources/Configuracion.json");
final String origin = (String) conf.getJson().get("origen") +args[0];
final String destiny = (String) conf.getJson().get("destino")+args[1];
FileInputStream fstream = new FileInputStream(origin);
FileWriter writer = new FileWriter(destiny);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
if(strLine.contains("Fecha de Emision: ")){
String date = strLine.substring(83,93);
SimpleDateFormat format = new SimpleDateFormat("MM/dd/YYYY");
String dateString = format.format(new Date(date));
String newDate = strLine.replace(date, dateString);
writer.write(newDate+"\n");
}
else{
writer.write(strLine+"\n");
}
}
br.close();
writer.close();
System.out.println("El programa ha finalizado con exito");
System.out.println("El nuevo archivo ha sido generado en " + destiny);
} catch (FileNotFoundException ex) {
Logger.getLogger(DateFormater.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(DateFormater.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
No, there is a specific way to do the encoding change on the FileWriter class. To achieve what you are looking for you have to carry out your development in another way, for example:
It is important to keep in mind that writing the file in ISO-8859-1 does not guarantee the conversion of the written data, for example: if you read a data in UTF-8 in a variable and then write that data to the file, the conversion will NOT be carried out of the written bytes, you will first have to convert the information to ISO-8859-1 and then if you write the data, you could do the previous conversion like this: