How to find character encoding of a text file (.properties, .xml, …etc)

We need to make sure that all .properties, xml files are are in same encoding. Other wise app servers give trouble in starting and it is difficult to find out.
Note: Download and add jar in classpath from http://site.icu-project.org/

import java.io.File;

import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;

/**
 * This program provides file encoding for given file.
 *
 * Make sure that all files are using same encoding as parameter passed to JVM.
 *
 * http://userguide.icu-project.org/conversion/detection#TOC-CharsetDetector
 *
 */
public class EncodingUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String PATH = "e://folder_name//";

		try {
			printFileEncoding(PATH + "a.properties");
			printFileEncoding(PATH + "b.properties");
			printFileEncoding("D://path2//asdf.xml");
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	private static void printFileEncoding(String fileName) {
		try {
			File file = new File(fileName);
			file = new File(fileName);
			byte fileContent[] = new byte[(int) file.length()];
			checkEncoding(fileName, fileContent);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private static void checkEncoding(String fileName, byte[] byteData) {
		CharsetDetector detector;
		CharsetMatch match;

		detector = new CharsetDetector();

		detector.setText(byteData);
		match = detector.detect();

		StringBuffer sbData = new StringBuffer();
		sbData.append("fileName==>").append(fileName);
		sbData.append(" Confidence==>").append(match.getConfidence());
		sbData.append(" Encoding==>").append(match.getName());
		System.out.println(sbData.toString());
	}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s