This program write on 2 files.
In the right file the string is "IL RITROVO AL 1° PIANO"
In the wrong file the string is "IL RITROVO AL 1NUL PIANO".
In the second case, the "°" charater has wrong econding; how can I detect this case before I write it?
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class WrongWriter {
static File wrongFile = new File("C:/Users/utente/Desktop/wrongFile.txt");
static File rightFile = new File("C:/Users/utente/Desktop/rightFile.txt");
public static void main(String[] args) throws IOException {
byte[] wrongBytes = new byte[]{
73, 76, 32, 82, 73, 84, 82, 79, 86, 79, 32, 65, 76, 32, 49, 0, 32, 80, 73, 65, 78, 79, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
};
write(wrongFile, wrongBytes) ;
byte[] rightBytes = "IL RITROVO AL 1° PIANO".getBytes();
write(rightFile, rightBytes) ;
}
static void write(File file, byte[] bytes) throws IOException{
OutputStreamWriter stream = null; //10227
stream = new OutputStreamWriter( new FileOutputStream( file ) , "ISO-8859-15");
stream.write( new String( bytes ) );
stream.flush();
stream.close();
}
}