1

I'm creating a program that functions like a Japanese dictionary, so I'm needing to store Japanese word objects in a file. I'm currently trying to create a dat file that is unicoded, but I keep getting FileNotFound Exceptions. My main goal is being able to store my custom created word objects in a file that can store and read a object that contains Japanese text and a array of values. So if you know of anyway to solve this problem I would be very grateful!

This is my trial classes to test it out:

  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();
}
}

This is the class that I'm using to get my values for my Word object, and this is my word class:

  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;
}
}

From these two classes I want to store and read the created word object in a file, but I can't figure out the most convenient way.

11
  • 1
    you get FileNotFoundExceptionsbecause your files does not exist! Are you invocking the method read before write? If you create an empty file in the project directoty? Commented Jun 3, 2013 at 16:45
  • You need to explain what line is throwing the exception. Also, Unicode has nothing to do with this. Commented Jun 3, 2013 at 16:47
  • show us the complete stack trace, and you will see that the application is searching the file in another directory Commented Jun 3, 2013 at 16:47
  • @fGo why would it be searching the file in a directory different than the one it creates the file in? Commented Jun 3, 2013 at 16:48
  • @Martinho if it invokes read before store Commented Jun 3, 2013 at 16:49

2 Answers 2

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

should simply be

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

The file is binary (the Java object). Internal Strings will be kept in Unicode - no problem there.

new File(("test.dat","UTF-8"); in fact is new File("test.dat/UTF-8") and expecting a directory test.dat. Hence FileNotFound. See File.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @fGo, yes I now added the needed clarification.
i didn't spot that! my respect to you sir :) docs.oracle.com/javase/6/docs/api/java/io/…, java.lang.String)
0

I recommend you to use existing means of object (de)serialisation like XML or JSON. Or even the use of a database.

You could then utilize already existing big frameworks, if you want to extend you program e. g. with a search function etc.


Here you have an example with JAXB.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.