从特定包Java中读取txt文件

时间:2023-01-25 21:54:35

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

谢谢

4 个解决方案

#1


24  

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.

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

#2


10  

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!!

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

#3


5  

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)

#4


0  

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将起作用。

#1


24  

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.

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

#2


10  

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!!

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

#3


5  

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)

#4


0  

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将起作用。