파일 인코딩을 ANSI (windows-1252)에서 UTF8로 변경해야한다는 요구 사항이 있습니다. 나는 자바를 통해 그것을하기 위해 아래 프로그램을 썼다. 이 프로그램은 문자를 UTF8로 변환하지만 notepade ++에서 파일을 열면 인코딩 유형이 ANSI로 UTF8로 표시됩니다. 이 날 액세스 DB에서이 파일을 가져올 때 오류가 발생합니다. UTF8 인코딩 만있는 파일이 필요합니다. 또한 모든 편집기에서 파일을 열지 않고 파일을 변환해야합니다.Java 파일 인코딩 변환
공용 클래스 ConvertFromAnsiToUtf8 {
private static final char BYTE_ORDER_MARK = '\uFEFF';
private static final String ANSI_CODE = "windows-1252";
private static final String UTF_CODE = "UTF8";
private static final Charset ANSI_CHARSET = Charset.forName(ANSI_CODE);
public static void main(String[] args) {
List<File> fileList;
File inputFolder = new File(args[0]);
if (!inputFolder.isDirectory()) {
return;
}
File parentDir = new File(inputFolder.getParent() + "\\"
+ inputFolder.getName() + "_converted");
if (parentDir.exists()) {
return;
}
if (parentDir.mkdir()) {
} else {
return;
}
fileList = new ArrayList<File>();
for (final File fileEntry : inputFolder.listFiles()) {
fileList.add(fileEntry);
}
InputStream in;
Reader reader = null;
Writer writer = null;
try {
for (File file : fileList) {
in = new FileInputStream(file.getAbsoluteFile());
reader = new InputStreamReader(in, ANSI_CHARSET);
OutputStream out = new FileOutputStream(
parentDir.getAbsoluteFile() + "\\"
+ file.getName());
writer = new OutputStreamWriter(out, UTF_CODE);
writer.write(BYTE_ORDER_MARK);
char[] buffer = new char[10];
int read;
while ((read = reader.read(buffer)) != -1) {
System.out.println(read);
writer.write(buffer, 0, read);
}
}
reader.close();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
모든 포인터가 도움이 될 것입니다.
덕분에, 인 Ashish
[iconv] (http://www.fileformat.info/tip/linux/iconv.htm)을 사용해 보셨습니까? Windows 바이너리에 대한 링크도 있습니다. – 9000
"인코딩 유형이 ANSI를 UTF8로 표시했습니다"는 의미는 무엇입니까? 이 프로그램이 Windows-1252에서 UTF-8로 파일을 변환하지 않았다고 생각하는 이유는 무엇입니까? –
사용중인 Java 버전은 무엇입니까? – anstarovoyt