Read txt files from specific packages

I'm trying to read a text file in a specific package, but it can't be found I can read it and insert an absolute path, but I want to read it without inserting an absolute path

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

What should I do?

thank you

Solution

You can use

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

A few side notes: do not use capital letters in package names; Use the English name of the variable These are generally accepted practices and practices

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>