Android的 - 从外部JAR阅读txt文件 [英] Android - reading txt files from external jar

查看:117
本文介绍了Android的 - 从外部JAR阅读txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心JDK编译一个罐子。

I have a jar compiled with core jdk.

它包含一个Java类,它读取TXT文件。
它有一个测试,证明了这个类可以正常工作。

It contains a java class which reads a txt file. It has a test which proves that this class works correctly.

如果我有这个瓶子在我的Andr​​oid项目,然后调用java类,读取TXT文件。它失败报告:java.io.FileNotFoundException并添加一个'/',而我想要加载txt文件的路径

If I include this jar in my android project and then call the java class that reads the txt file. It fails reporting: java.io.FileNotFoundException and adding a '/' to the path of the txt file which I wanted to load.

是Android的安全模型被读取停止txt文件?

Is Android's security model stopping the txt file from being read?

我的项目结构为:

Android的模块:

的src / Loader.java [调用GetName.java]

Java模块:

测试/ TestGetName.java [调用GetName.java]

的src / GetName.java

资源/ names.txt中

摘要:

TestGetName.java作品

TestGetName.java works

Loader.java失败。出现FileNotFoundException在里面的GetName抛出

Loader.java fails. A FileNotFoundException is thrown inside GetName

推荐答案

在结束了它。我需要一个条件,看看我加载本地或作为资源文件。有可能是一个更合适的方法来做到这一点,而无需使用条件。

Figured it out in the end. I need a condition to see if I am loading the file locally or as a resource. There is probably a neater way to do this without using a conditional.

    String source = "resources/inputfile.txt";
    BufferedReader fin;

    InputStream inputStream = getClass().getResourceAsStream(source.substring(source.lastIndexOf("/")));
    if (inputStream != null) {
        fin = new BufferedReader(new InputStreamReader(inputStream));
    } else {
        fin = new BufferedReader(new FileReader(new File(source)));
    }

谢谢你的提示,关于阅读作为一种资源user77777777

Thanks for the hints about reading as a resource user77777777

这篇关于Android的 - 从外部JAR阅读txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆