Find non-ascii characters in Text string
5
jpereira1
Find binary, malformed or non ascii characters in a file.
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharacterCodingException;
public class TestAscii {
public static void main (String args[])
throws Exception {
BufferedReader re =
new BufferedReader(new FileReader("infile.txt"));
String test = re.readLine();
byte bytearray [] = test.getBytes();
System.out.println("Test string : " + test);
CharsetDecoder d = Charset.forName("US-ASCII").newDecoder();
try {
CharBuffer r = d.decode(ByteBuffer.wrap(bytearray));
r.toString();
}
catch(CharacterCodingException e) {
System.out.println("only regular ASCII characters please!");
// interrupt the processing
throw new Exception(e);
}
System.out.println("Ok, it's ASCII only!");
}
}
Comments
Thu. Sep. 21st, 2006 1:38 PM
bertheymans
bertheymans





