2013-10-27 94 views
0

配偶!RandomAccessFile閱讀西里爾文UTF-8 java

我無法使用RandomAccessFile從西里爾文本中讀取文件。

下面是使用這種格式的一個簡單的程序,在特定的文件中寫入信息(西里爾字):

keyLength, valueLength, key, value

然後程序嘗試讀取這些信息,但是我的輸出是不正確的:

writing success 
keyLength = 10, valueLength = 4 
read: килло, гр 

UPD 預期輸出:

writing success 
keyLength = 10, valueLength = 4 
read: киллограмм, сала 

什麼問題? (除了問題,我有小的大腦)

import java.io.FileNotFoundException; 
import java.io.RandomAccessFile; 
import java.io.IOException; 

public class Main { 

    public static void main(String[] args) { 
     String fileName = "file.db"; 
     RandomAccessFile outputFile = null; 

     try { 
      outputFile = new RandomAccessFile(fileName, "rw"); 
     } catch (FileNotFoundException e) { 
      System.err.println(e.getMessage()); 
      System.exit(1); 
     } 

     String key = "киллограмм"; 
     String value = "сала"; 

     try { 
      outputFile.writeInt(key.length()); 
      outputFile.writeInt(value.length()); 

      outputFile.write(key.getBytes("UTF-8")); 
      outputFile.write(value.getBytes("UTF-8")); 
     } catch (IOException e) { 
      System.err.println(e.getMessage()); 
      System.exit(1); 
     } 

     System.out.println("writing success"); 

     RandomAccessFile inputFile = null; 

     try { 
      inputFile = new RandomAccessFile(fileName, "r"); 
     } catch (FileNotFoundException e) { 
      System.err.println(e.getMessage()); 
      System.exit(1); 
     } 

     int keyLength = 0, valueLength = 0; 

     try { 
      keyLength = inputFile.readInt(); 
      valueLength = inputFile.readInt(); 
     } catch (IOException e) { 
      System.err.println(e.getMessage()); 
     } 

     System.out.println("keyLength = " + keyLength + ", valueLength = " + valueLength); 
     if (keyLength <= 0 || valueLength <= 0) { 
      System.err.println("key or value length is negative"); 
      System.exit(1); 
     } 

     byte[] keyBytes = null, valueBytes = null; 

     try { 
      keyBytes = new byte[keyLength]; 
      valueBytes = new byte[valueLength]; 
     } catch (OutOfMemoryError e) { 
      System.err.println(e.getMessage()); 
      System.exit(1); 
     } 

     try { 
      inputFile.read(keyBytes); 
      inputFile.read(valueBytes); 
     } catch (IOException e) { 
      System.err.println(e.getMessage()); 
      System.exit(1); 
     } 

     try { 
      System.out.println("read: " + new String(keyBytes, "UTF-8") + ", " + new String(valueBytes, "UTF-8")); 
     } catch (IOException e) { 
      System.err.println(e.getMessage()); 
      System.exit(1); 
     } 

    } 
} 
+1

您的預期產量是多少? –

+0

@SotiriosDelimanolis,upd – NinjaTurtle

+0

如果您按順序讀取/寫入,'java.io.Writer'和'Reader'會封裝一個OutputStream/InputStream併爲您處理UTF-8或其他字符編碼。你確定這些不會是更好的解決方案嗎? –

回答

2

的問題是這個

outputFile.writeInt(key.length()); 

String#length()

返回此字符串的長度。長度等於字符串中Unicode代碼單元的數字 。

在這種情況下,它返回值10,它不是表示此String所需的字節數。

你想要的是

key.getBytes("UTF-8").length 

用作

byte[] keyBytes = key.getBytes("UTF-8"); 
outputFile.writeInt(keyBytes.length); 

同爲value

+0

謝謝,不錯的一個! – NinjaTurtle

+0

@NinjaTurtle另外,你有很多'try-catch'塊。你爲什麼不把所有的代碼都包裝在一個模塊中呢?無論如何,你總是退出應用程序。 –