android字典类书籍,Android Java - 阅读字典资产

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

我有这个方法,它读取字典并用它进行解密 . 但是,我不知道如何让它读取字典 . 我将字典拖放到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){

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值