Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading txt file from a specific package Java

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

like image 340
dextervip Avatar asked Dec 05 '10 21:12

dextervip


1 Answers

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.

like image 73
Bozho Avatar answered Sep 24 '22 13:09

Bozho