[JAVA] 파일 인코딩 확인하기

Posted by 김성철

JAVA - 파일 인코딩 확인 하기

라이브러리

https://mvnrepository.com/artifact/com.googlecode.juniversalchardet/juniversalchardet/1.0.3  

사용법

=================================================================================================================  
public String readEncoding(File file) throws IOException {  
	byte[] buf = new byte[4096];  
	java.io.FileInputStream fis = new java.io.FileInputStream(file);  
	UniversalDetector detector = new UniversalDetector(null);  
	int nread;  
	while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {  
		detector.handleData(buf, 0, nread);  
	}  
	detector.dataEnd();  
	String encoding = detector.getDetectedCharset();  
	detector.reset();  
	buf = null;  
	fis.close();  
	return encoding == null?"UTF-8":encoding;  
}  
  
=================================================================================================================