2014-11-07 6 views
0

텔넷을 통해 데이터베이스와 통신하는 프로그램이 있는데 이전에는 System.out.println() 콘솔에 응답을 인쇄했습니다. 이제 프로그램을 수정하여 응답을 파일에 기록하므로 프로그램을 서비스/데몬으로 실행할 수 있습니다. 그러나 BufferedWriter를 사용할 때 나는 영어를 얻지 못합니다. 파일 탐색기에서 파일의 미리보기가 올바르게 읽히지 만 파일을 메모장 또는 숭고한 텍스트 2에서 열면이 이상한 조합이 생깁니다. 여기 내 코드와 내 파일을 열 때 얻을 수있는 것과 반대로 파일이 말해야하는 것입니다.BufferedWriter가 문자열을 올바르게 쓰지 못함

private static void logon() throws IOException { 
    bout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/mnt/javaprograms/ServerConsole/log.txt"), "UTF-8")); 

    String loginString = "JAVA-TRANS\n"; 
    byte[] logon = loginString.getBytes(); 
    out.write(logon); 
    out.flush(); 
    out.write(logon); 
    out.flush(); 
    response = in.readLine(); 
    bout.write(response); 
    bout.flush(); 
    while (!response.contains("OK")) { 
     response = in.readLine(); 
     bout.write(response); 
     bout.flush(); 
    } 
    bout.flush(); 
    bout.close(); 
} 

파일 탐색기 미리보기 다음 mvBASE 텔넷 server.You에

오신 것을 환영합니다-TRANS에게 MILL6JAVA 에

열린 파일을 54 행으로 연결되어 읽

5765 6c63 6f6d 6520 746f 2074 6865 206d 
7642 4153 4520 7465 6c6e 6574 2073 6572 
7665 722e 596f 7520 6172 6520 636f 6e6e 
6563 7465 6420 746f 206c 696e 6520 3534 
206f 6e20 4d49 4c4c 364a 4156 412d 5452 
414e 5300 4f4b 

답변

0

이유가 무엇이든간에 텍스트 편집기는 di 기본이되는 바이트의 16 진수 표현을 나타냅니다. 적절한 문자 세트에 텍스트 표현을 표시하려면이를 변경해야합니다. 예를 들어

public static void main(String[] args) throws Exception { 
    String text = "Welcome to the mvBASE telnet server.You are connected to line 54 on MILL6JAVA-TRANS"; 
    byte[] bytes = text.getBytes(StandardCharsets.UTF_8); 
    int space = 0; 
    for (byte b : bytes) { 
     System.out.print(Integer.toHexString(b)); 
     space++; 
     if (space == 16) { 
      System.out.println(); 
      space = 0; 
     } else if (space % 2 == 0) { 
      System.out.print(" "); 
     } 
    } 
} 

인쇄

당신이 (+/- 3 문자)를 참조하십시오 것입니다
5765 6c63 6f6d 6520 746f 2074 6865 206d 
7642 4153 4520 7465 6c6e 6574 2073 6572 
7665 722e 596f 7520 6172 6520 636f 6e6e 
6563 7465 6420 746f 206c 696e 6520 3534 
206f 6e20 4d49 4c4c 364a 4156 412d 5452 
414e 53 

.

+0

헥스를 표시하지 않도록 텍스트 편집기 (하위 텍스트 2)를 편집했습니다. 나는 ST2와 Windows 메모장이 그 16 진수를 보여 주었기 때문에 내가 잘못하고있는 일이있을 것이라고 생각했다. – andrewcole50