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.
FileNotFoundExceptionsbecause your files does not exist! Are you invocking the methodreadbeforewrite? If you create an empty file in the project directoty?