这可能是一个重复的线程但是在尝试了几个和多个线程之后我的问题仍然存在:/
我有这个方法,它读取字典并用它进行解密 . 但是,我不知道如何让它读取字典 . 我将字典拖放到assets文件夹,然后使用此代码 .
try {
System.out.println("Dictionary Initialize");
BufferedReader inputReader;
FileInputStream fis = new FileInputStream("/assets/dictionary-english.txt");
BufferedReader dictionary = new BufferedReader(new InputStreamReader(fis));
String checktext;
while ((checktext = dictionary.readLine()) != null) {
dictionaryset.add(checktext);
}
System.out.println("Dictionary Successfully Initialized");
} catch (IOException ex) {
System.out.println("Error! reading!");
}
经过多次测试后,我确认它甚至无法到达while循环,因为try和catch捕获了错误 . 它也发现了 IOException 错误 . 知道如何解决这个问题吗?谢谢
这是我从 logcat 的错误
10-06 14:54:50.555: W/System.err(1980): java.io.FileNotFoundException: /assets/dictionary-english.txt (No such file or directory)
10-06 14:54:50.555: W/System.err(1980): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
10-06 14:54:50.555: W/System.err(1980): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
10-06 14:54:50.555: W/System.err(1980): at java.io.FileInputStream.(FileInputStream.java:80)
10-06 14:54:50.565: W/System.err(1980): at java.io.FileInputStream.(FileInputStream.java:132)
编辑:尝试使用
AssetManager assetManager = getAssets();
但是我在 getAssets() 上收到错误,这是 "Cannot make a static reference to the non-static method getAssets() from the type ContextWrapper"
================================================== ======================
回答:
BufferedReader dictionary = new BufferedReader(new InputStreamReader(getAssets().open("dictionary-english")));
我用这行代码来获取我的文件,并在方法中删除了 static ,这解决了我无法做出静态引用的问题
eg; private static String run1(String cipherText){ --> private String run1(String cipherText){