2013-08-09 4 views
4

나는 그것이 ANSI 인코딩 인 텍스트 파일을 가지고 있는데, UTF8 인코딩으로 변환해야합니다.Java에서 utf8로 ANSI를 변환하는 방법은 무엇입니까?

내 텍스트 파일은 UTF8에서 같은 문자 인코딩이 Stochastic programming is an area of mathematical programming that studies how to model decision problems under uncertainty. For example, although a decision might be necessary at a given point in time, essential information might not be available until a later time.

+0

'ANSI 인코딩은 시스템의 표준 코드 페이지를 참조하는 데 사용되는 약간 일반적인 용어입니다. 즉, 실행중인 시스템의 로케일에 따라 다릅니다. _ASCII_를 의미하면 표준 ASCII 문자 (<128)가 UTF-8의 동일한 인코딩에 매핑되므로 텍스트가 이미 모두 있습니다. –

답변

0

ASCII 문자 집합지도처럼, 그래서 파일이 정말 어떤 변환을 필요로하지 않는다.

PrintWriter out = new PrintWriter(new File(filename), "UTF-8"); 
out.print(text); 
out.close(); 
+0

나는 이것을 시도하지만 ASCII에서 UTF-8로 변환하지 않는다. –

+0

내 말은 변환 할 것이 없다는 뜻이다. ASCII 파일은 이미 UTF-8을 준수합니다. – Lake

+0

죄송합니다. ANSI to UTF8 –

0

당신은 내가 전문가가 아니다이

InputStream inputStream = new BufferedInputStream(new FileInputStream("D:\\sample.txt")); 
    Reader reader = 
      new InputStreamReader(inputStream, Charset.forName("UTF-8")); 
5

당신은 java.nio.charset.Charset 클래스에 명시 될 수있다 (창-1252 ANSI에 대한 적절한 이름) :

public static void main(String[] args) throws IOException { 
    Path p = Paths.get("file.txt"); 
    ByteBuffer bb = ByteBuffer.wrap(Files.readAllBytes(p)); 
    CharBuffer cb = Charset.forName("windows-1252").decode(bb); 
    bb = Charset.forName("UTF-8").encode(cb); 
    Files.write(p, bb.array()); 
} 

아니면 =를 선호하는 경우 한 줄)에

Files.write(Paths.get("file.txt"), Charset.forName("UTF-8").encode(Charset.forName("windows-1252").decode(ByteBuffer.wrap(Files.readAllBytes(Paths.get("file.txt"))))).array()); 
관련 문제