1

我正在创建一个功能类似于日语词典的程序,因此我需要将日语单词对象存储在一个文件中。我目前正在尝试创建一个未编码的 dat 文件,但我不断收到 FileNotFound 异常。我的主要目标是能够将我自定义创建的单词对象存储在一个文件中,该文件可以存储和读取包含日语文本和一组值的对象。因此,如果您知道无论如何要解决此问题,我将不胜感激!

这是我的试用课程来测试它:

  public class JavaApplication1 {

/**
 * @param args the command line arguments
 */
Scanner scan = new Scanner(System.in);
//File file = new File("test.dat");
public static void main(String[] args) throws FileNotFoundException, IOException,    ClassNotFoundException {
    // TODO code application logic here
    JavaApplication1 ja = new JavaApplication1();
    ja.start();
}
public void start() throws FileNotFoundException, IOException, ClassNotFoundException{

    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();
    //List<int> typeOfWord = new ArrayList<int>();
    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());
    }
    Word word = new Word(Kanji,Romanji,common,type,adjective,verb,radical);
    store(word);
    //store(word);
    read();

}
public void store(Word word) throws FileNotFoundException, IOException{
    File file = new File("test.dat","UTF-8");
    FileOutputStream outFileStream = new FileOutputStream(file);
    ObjectOutputStream oos = new ObjectOutputStream(outFileStream);
    oos.writeObject(word);
    oos.close();
}
public void read() throws FileNotFoundException, IOException, ClassNotFoundException{
    File file = new File("test.dat");
    FileInputStream filein = new FileInputStream(file);
    ObjectInputStream ois = new ObjectInputStream(filein);
    Word word = (Word)ois.readObject();
    ois.close();
    word.getKanaKanji();
}
}

这是我用来获取我的 Word 对象值的类,这是我的 word 类:

  public Word(){

}
public Word(String kanaKanji, String romanji, int commonType, ArrayList wordTypeList,
        int adjectiveType, int verbType, ArrayList radicalList){
    this.wordTypeList = wordTypeList;
    this.radicalList = radicalList;
    this.adjectiveType = adjectiveType;
    this.commonType = commonType;
    this.verbType = verbType;
    this.kanaKanji = kanaKanji;
    this.romanji = romanji;

}
//slew of get methods
public String getKanaKanji(){
 return kanaKanji;   
}
public String getRomanji(){
    return romanji;
}
public StringBuilder getDefinition(){
    int size = definitionList.size();
    StringBuilder definitions = new StringBuilder();
    for(int i = 0; i <definitionList.size();i++){
        definitions.append((i+1)+"."+definitionList.indexOf(i) +"\n");
    }
    return definitions;
}
}

从这两个类中,我想在文件中存储和读取创建的 word 对象,但我想不出最方便的方法。

4

2 回答 2

5
File file = new File("test.dat","UTF-8");

应该只是

File file = new File("test.dat");

该文件是二进制文件(Java 对象)。内部字符串将保存在 Unicode 中 - 没有问题。

new File(("test.dat","UTF-8");实际上是new File("test.dat/UTF-8")并期待一个目录test.dat。因此 FileNotFound。请参阅文件

于 2013-06-03T16:48:20.763 回答
0

我建议您使用现有的对象(反)序列化方法,如 XML 或 JSON。甚至使用数据库。

然后,如果您想扩展您的程序,例如使用搜索功能等,您可以利用已经存在的大型框架。


这里有一个JAXB 示例

于 2013-08-21T16:29:02.033 回答