0

テキストを1行ずつ読み取り、文字列バッファーに5000ワードごとに文字列バッファーを埋めるサーブレット言語を使用した私のコードは、jspページにコンテンツを表示し、ヘッダー表示テキストをutf-8に入れます。問題は、テキストがコードをutf-8エンコーディングするとunknown文字で読み取られ、コードでテキストをutf-8で読み取り、utf-8エンコーディングでユーザーに表示されるテキストがテキストを読み取るためのコードです

 try{
   File file = new File(p);         
   l = (int) file.length(); 
   String s = null,strr=null;
   String g = null,m=null;
   int i = 0;
   Scanner input = new Scanner(file); 
   s1 = new StringBuffer();
   s2 = new StringBuffer();
   while (input.hasNextLine()) {
     g = input.nextLine();
     String[] d = g.split(" ");
     int h = d.length;
     i = i + h;
     if (i > 5000) {
       break;
     }
     s1.append(g).append(System.getProperty("line.separator"));
   }
   if(i > 5000) {
     s2.append(g).append(System.getProperty("line.separator"));
     while( input.hasNextLine()) {
     s = input.nextLine(); 
     String[] d = s.split(" ");
     int h = d.length;
     i=0;
     i=i+h;
     if (i > 5000) {
       break;
     }
     s2.append(s).append(System.getProperty("line.separator"));}
     if (input.hasNextLine()) {
       s3.append(s).append(System.getProperty("line.separator"));
       while(input.hasNextLine()) {
         strr = input.nextLine(); 
         s3.append(strr).append(System.getProperty("line.separator"));
       }
     }
   }
 input.close(); 
 } catch (Exception e) {
   e.getMessage();
 }
4

1 に答える 1

0

使用しているメソッドのjavadocsを確認してください。Scanner (File)コンストラクターは次のことを通知します。

ファイルのバイトは、基盤となるプラットフォームのデフォルトの文字セットを使用して文字に変換されます。

代わりに、Scanner(File、String)コンストラクターを呼び出して、ファイルのエンコードを明示的に指定できます。

Scanner input = new Scanner(file, "UTF-8");
于 2012-07-02T10:56:17.250 に答える