我在将日文文本转换为可读文本时遇到问题。现在我有一个试用程序正在从用户那里获取值。然后将这些值传递给我称为word的类以创建对象。一旦创建了对象,我就想要将对象写入并读取到文件中。由于我正在阅读和编写对象,因此我使用objectouput和输入流来执行此操作。这个问题是我不确定在使用objectoutput和输入流时如何使用UTF-8编码文件。如果我不使用任何编码,我会得到问号,假名或汉字应该是。
无论如何要使用和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方法时,我会得到问号。
我确实有一个支持日文字符的操作系统,所以这不是问题。
提前谢谢!
答案 0 :(得分:0)
当您通过ObjectOutputStream编写String对象时,首先,String对象的长度以2个字节写入,然后String对象的内容以修改后的UTF-8编写。请参阅DataOutput.writeUTF(String)的说明。
http://docs.oracle.com/javase/7/docs/api/java/io/DataOutput.html#writeUTF%28java.lang.String%29
您看到的问号是前2个字节,表示字符串的长度。