Android Java - 阅读字典资产

时间:2013-10-06 14:48:59

标签: java android eclipse

这可能是一个重复的线程但是在尝试了几个和多个线程之后我的问题仍然存在:/

我有这个方法,它读取字典并用它进行解密。但是,我不知道如何让它读取字典。我将字典拖放到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.<init>(FileInputStream.java:80)
10-06 14:54:50.565: W/System.err(1980):     at java.io.FileInputStream.<init>(FileInputStream.java:132)

编辑:尝试使用

AssetManager assetManager = getAssets();

但是我在getAssets()上出现错误“无法对ContextWrapper类型的非静态方法getAssets()进行静态引用”

=============================================== =========================

答案:

BufferedReader dictionary = new BufferedReader(new InputStreamReader(getAssets().open("dictionary-english")));    

我使用这行代码来获取我的文件,并在方法中删除了static,这解决了我无法生成静态引用问题

eg;   private static String run1(String cipherText){  --> private String run1(String cipherText){

0 个答案:

没有答案