2013-06-03 46 views
1

我在将日文文本转换为可读文本时出现问题。现在我有一个从用户那里获得价值的试用程序。然后这些值通过一个我称为word的类来创建一个对象。一旦对象被创建,我想要将对象写入并读取到文件中。由于我正在读写对象,因此我正在使用对象输入和输入流来执行此操作。这个问题是我不确定如何使用对象输出和输入流使用UTF-8编码文件。如果我不使用任何编码,我会在假名或汉字应该出现的地方得到问号。unicode中的日文文本

反正有没有使用和objectoutput或输入流将文件转换为unicode。如果没有,有没有其他方法可以避免在假名或汉字应该出现的地方出现问号?

public class JavaApplication1 { 

    /** 
    * @param args the command line arguments 
    */ 

    Scanner scan = new Scanner(System.in); 

    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException, FontFormatException { 
     // TODO code application logic here 
     JavaApplication1 ja = new JavaApplication1(); 
     ja.start(); 
    } 
    public void start() throws FileNotFoundException, IOException, ClassNotFoundException, FontFormatException{ 

     System.out.println("Enter Kanji"); 
     String Kanji = scan.next(); 
     System.out.println("Enter Romanji"); 
     String Romanji = scan.next(); 
     System.out.println("How common is it"); 
     int common = scan.nextInt(); 
     System.out.println("How many types of word is it?"); 
     int loop = scan.nextInt(); 
     ArrayList type = new ArrayList(); 
     for(int i = 0; i<loop;i++){ 
      System.out.println("What type of word"); 
      type.add(scan.nextInt()); 
     } 
     System.out.println("What type of adjective"); 
     int adjective = scan.nextInt(); 
     System.out.println("What type of verb"); 
     int verb = scan.nextInt(); 
     System.out.println("How many radicals"); 
     int loop2 = scan.nextInt(); 
     ArrayList radical = new ArrayList(); 
     for(int i = 0; i<loop2;i++){ 
      System.out.println("radical"); 
      radical.add(scan.nextInt()); 
     } 
     //String newKanji = GetUnicode(Kanji); 
     Word word = new Word(Kanji,Romanji,common,type,adjective,verb,radical); 
     word.getKanaKanji(); 
     store(word); 
     //store(word); 
     read(); 

    } 
    public void store(Word word) throws FileNotFoundException, IOException, FontFormatException{ 
     File file = new File("test.dat"); 
     FileOutputStream outFileStream = new FileOutputStream(file); 
     ObjectOutputStream oos = new ObjectOutputStream(outFileStream); 
     oos.writeObject(word); 
     oos.close(); 
    } 
    public void read() throws FileNotFoundException, IOException, ClassNotFoundException, FontFormatException{ 
     File file = new File("test.dat"); 
     FileInputStream filein = new FileInputStream(file); 
     ObjectInputStream ois = new ObjectInputStream(filein); 
     Word word = (Word) ois.readObject(); 
     ois.close(); 
     System.out.println(word.getKanaKanji());//this gets the kanakanji 

    } 
} 

当我调用Word类的getKanaKanji方法时,我得到了问号。

我确实有支持日文字符的操作系统,所以这不是问题。

预先感谢您!

+1

我真的不明白。如果你写一个Object到文件,它应该是二进制的,因此根本没有任何“字符串编码”。如果您编写了文本文件,则编码问题将适用。问号通常暗示所使用的* font *没有所需的字符。你检查了吗?也许你将不得不使用支持所有(或至少你需要的)unicode代码点的字体。 – Fildor

+0

我倾向于同意Fildor。我没有看到任何使用字符编码的地方。这可能是您的控制台应用程序的问题。 – Aurand

+1

一个简单的测试就是在写入磁盘之前和之后将对象打印到控制台。它会改变吗? – Aurand

回答