[英]How to read a file that i created inside my the same package?
This is a chunk of data I'd like to access by a method. 这是我想通过一种方法访问的数据块。 I'm doing the following to read my file:
我正在执行以下操作来读取我的文件:
String fileName = "file.txt"
InputStream inputStream = new FileInputStream(fileName);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
My file.txt is in the same package, but I still get FileNotFoundException. 我的file.txt在同一个程序包中,但是我仍然得到FileNotFoundException。 I didn't use a path url to point to the file because I thought since this it going to be an android application, hard-coding the path might not work when deployed... Please correct me if I am wrong.
我没有使用路径url指向文件,因为我认为这将是一个android应用程序,因此在部署时可能无法对路径进行硬编码...如果我错了,请更正我。 Thanks bunch!
谢谢一群!
This shows how to do that. 这说明了如何做到这一点。 https://stackoverflow.com/a/14377185/2801237
https://stackoverflow.com/a/14377185/2801237
Also the 'package' your class is in has nothing to do with the 'path' where the file is being executed from. 同样,您的类所在的“包”与从中执行文件的“路径”无关。 (two different concepts, 'package' = folder hierarchy of java source code files), 'path' = location on a filesystem of a specific file, your APK is being 'executed' in a particular place, and the location it writes a file is associated with that (I actually don't know where 'offhand' it writes by default, because I always get cache dir, or sd card root, etc.)
(两个不同的概念,“ package” = Java源代码文件的文件夹层次结构),“ path” =特定文件在文件系统上的位置,您的APK正在特定位置“执行”,并且它写入文件的位置与之相关联(我实际上不知道默认情况下它会写在哪里,因为我总是得到缓存目录或sd卡根目录等)。
您可以使用:
InputStream inputStream = this.getClass().getResourceAsStream(fileName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.