Find non-ascii characters in Text string





5
Date Submitted Sun. Sep. 10th, 2006 8:35 AM
Revision 1 of 1
Beginner jpereira1
Tags - | ASCII | binary | characters | encoding | malformed
Comments 1 comments
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!");
  }
}
 

julian pareira

Comments

Comments tag the language
Thu. Sep. 21st, 2006 1:38 PM    Scripter bertheymans

Voting