Trasformare un file UTF8 in ASCII

Il problema dell'encoding è molto diffuso in informatica, specialmente in ambienti internazionali, in cui le informazioni fluiscono da e tra diversi paesi. E specialmente nel Web. Vediamo qui come si può trasformare un file UTF-8 in ASCII. La seguente procedura trasforma una stringa UTF-8 in una stringa ASCII:


/**
 * Convert a UTF8 String to an ASCII String
 * @param utf8String the source string
 * @return the ASCII string
 */
 public String convertStringToAscii(String utf8String){
    final CharsetEncoder asciiEncoder = 
    		Charset.forName("US-ASCII").newEncoder();
    final StringBuilder result = new StringBuilder();
    for (final Character character : utf8String.toCharArray()) {
        if (asciiEncoder.canEncode(character)) {
            result.append(character);
        } else {
            result.append("\\u");
            result.append(Integer.toHexString(0x10000 | character)
            	.substring(1).toUpperCase());
        }
    }
    return new String(result);
}			

A partire da un file UTF-8, di cui si conosce il percorso, è possibile estrarre la stringa corrispondente e tradurla in ASCII così:


/**
 * Take a UTF8 file and generate an ASCII String
 * @param filePath the UTF8 filepath
 * @return the ASCII String of the file
 * @throws Exception
 */
public String convertFileUTF8ToAsici(String filePath) throws Exception{
	/* Read in the utf8 file as a byte array from an input stream */
	FileInputStream fis = new FileInputStream(filePath);
	byte[] utf8Contents = new byte[fis.available()];
	fis.read(utf8Contents);
	fis.close();
	/* Convert the byte array to a utf8 string */
	String utf8String = new String(utf8Contents, "UTF8");
	return convertStringToAscii(utf8String);
}		

A questo punto basta scrivere la stringa ASCII in un file per ottenere il file ASCII!