从特定的 Java 包中读取 txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4361338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:29:33  来源:igfitidea点击:

Reading txt file from a specific package Java

javafiletextpackage

提问by dextervip

I'm trying to read a text file in a specific package but it return that couldn't be found.I can read it inserting the absolute path but i want to read it without inserting the absolute path.

我正在尝试读取特定包中的文本文件,但它返回找不到。我可以通过插入绝对路径来读取它,但我想在不插入绝对路径的情况下读取它。

String texto = "Utils/CEP/Cidades/" + estado + ".txt";
FileReader fr = new FileReader(texto);
BufferedReader in = new BufferedReader(fr);

How should i do?

我应该怎么做?

Thanks

谢谢

采纳答案by Bozho

You can use

您可以使用

InputStream in = 
   getClass().getResourceAsStream("/Utils/CEP/Ciades/" + estado + ".txt");
Reader fr = new InputStreamReader(in, "utf-8");

A few sidenotes: don't use capital letters in package names; use English names of your variables. These are accepted practices and conventions.

一些旁注:不要在包名中使用大写字母;使用变量的英文名称。这些是公认的做法和惯例。

回答by Codemwnci

If the text file exists within the same structure as your class files, then you may be better suited using getResourceAsStream.

如果文本文件与您的类文件存在于相同的结构中,那么您可能更适合使用 getResourceAsStream。

http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

回答by dspyz

For total portability, consider using File.separator in place of forward slashes, but yeah getResourceAsStream should work. Keep in mind, that if you're working in eclipse, your class files will probably be in bin with respect to your working directory so if it's just in your project folder, the way you have it should work, but not getResourceAsStream. Alternatively, if the resource you want to access is in a source folder, it will be copied into bin whenever you clean your project so getResourceAsStream will work.

为了整体的可移植性,请考虑使用 File.separator 代替正斜杠,但是 getResourceAsStream 应该可以工作。请记住,如果您在 eclipse 中工作,您的类文件可能会在相对于您的工作目录的 bin 中,因此如果它只是在您的项目文件夹中,那么您拥有它的方式应该可以工作,而不是 getResourceAsStream。或者,如果您要访问的资源位于源文件夹中,则每当您清理项目时,它都会被复制到 bin 中,以便 getResourceAsStream 起作用。

回答by Vivek

It could be little late still this could help many other. These are ways to access the resources available in the project

这可能会为时已晚,这可以帮助许多其他人。这些是访问项目中可用资源的方法

Getting resources form the default package

从默认包中获取资源

// Getting Resource as file object
File f = new File(getClass().getResource("/excludedir.properties").getFile());

// Getting resource as stream object
InputStream in = getClass().getResourceAsStream("/excludedir.properties");

Getting resources from specific packages

从特定包中获取资源

// Getting Resource as file object
File f = new File(getClass().getResource("/com/vivek/core/excludedir.properties").getFile());

// Getting resource as stream object
InputStream in = getClass().getResourceAsStream("/com/vivek/core/excludedir.properties");

Note :getclass() is a non-static function which cannot be called form the static context. If you want to call from the static context use

注意:getclass() 是一个非静态函数,不能从静态上下文中调用。如果您想从静态上下文中调用,请使用

YourClassName.class.getResource("/com/vivek/core/excludedir.properties").getFile()

Hope this helps. Cheers!!

希望这可以帮助。干杯!!