2016-04-05 15 views
0

Javaを使用してデータベースを新しいスキーマに移行および解析しようとしています。 問題は、特にJavaでデータを扱いながら混乱するいくつかの文字、特にarabがあることです。Javaを使用してアラビア文字を解析する問題

:私はそれを解析した後

(4, 'Afganistán', 1, 'Afgano', 'Afghanistan', 'AF', 'أفغانستان', 'Afghan', 'أفغاني');

、countryParsed.sqlで結果の行は次のように見られている。ここで

は、私がcountryToParse.sqlファイルにしているとのトラブルを取得していますラインの一つです

(4, 'Afganistán', 1, 'Afgano', 'Afghanistan', 'AF', 'أ�?غانستان', 'Afghan', 'أ�?غاني');

アラブ文字がどのように乱れているかがわかります。
ファイルを開くと、それらが両方ともUTF-8でコード化されていることを確認できます。

私が使用しているJavaコードは次のとおりです。方法writeToTextFile()では、私はあなたがこの中にエンコーディングを設定するのを忘れ、私はUTF-8を使用してファイルを書き込むためにある3つの方法(私は彼らに三つの方法を使用して同じエラーを取得しています言及していない)

public class MainStackOverflow { 

public static void main(String[] args) throws IOException { 

    String countryStr = new   String(readTextFile("src/data/countryToParse.sql").getBytes(), "UTF-8"); 
    writeToTextFile("src/data/countryParsed.sql", countryStr); 
} 

    public static String readTextFile(String fileName) throws IOException { 
    String content = new String(Files.readAllBytes(Paths.get(fileName))); 
     return content; 
    } 

    public static void writeToTextFile(String fileName, String content) throws IOException { 

     /* Way 1 */ 
     Files.write(Paths.get(fileName), content.getBytes("UTF-8"), StandardOpenOption.CREATE); 


     /* Way 2 */ 
     BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
       new FileOutputStream(fileName), "UTF-8")); 
      try { 
       out.write(content); 
      } finally { 
       out.close(); 
      } 

     /* Way 3 */ 
     PrintWriter out1 = new PrintWriter(new File(fileName), "UTF-8"); 
     out1.write(content); 
     out1.flush(); 
     out1.close(); 
    /* */ 
    } 
} 

答えて

0

を追加しましたライン:

String content = new String(Files.readAllBytes(Paths.get(fileName))); 

は、単にこれを試してみてください:

public static void main(String[] args) throws IOException { 

    String countryStr = new String(readTextFile("src/data/countryToParse.sql"), "UTF-8"); 
    writeToTextFile("src/data/countryParsed.sql", countryStr); 
} 

public static byte[] readTextFile(String fileName) throws IOException { 
    return Files.readAllBytes(Paths.get(fileName)); 
} 
+0

を解決!ありがとうNicolas – miguels

+0

@miguels良いニュース! –

関連する問題