Tai*_*kun 1 java filenotfoundexception
我正在创建一个类似日语字典的程序,所以我需要将日文单词对象存储在一个文件中.我目前正在尝试创建一个无线编码的dat文件,但我一直在获取FileNotFound Exceptions.我的主要目标是能够将我自定义创建的单词对象存储在一个文件中,该文件可以存储和读取包含日文文本和值数组的对象.所以如果你知道无论如何要解决这个问题,我将非常感激!
这是我的试用课来测试它:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我用来获取我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
从这两个类我想要存储和读取文件中创建的单词对象,但我无法找出最方便的方法.
File file = new File("test.dat","UTF-8");
Run Code Online (Sandbox Code Playgroud)
应该只是
File file = new File("test.dat");
Run Code Online (Sandbox Code Playgroud)
该文件是二进制文件(Java对象).内部字符串将保留为Unicode - 没有问题.
new File(("test.dat","UTF-8");实际上是new File("test.dat/UTF-8")并期待一个目录test.dat.因此FileNotFound.见文件.
| 归档时间: |
|
| 查看次数: |
1209 次 |
| 最近记录: |