Problem when reading txt file with java

-1

I have a Configuration package where I have a Config.java class and a config.txt file that I want to read.

Config.java

public class Config {


public String obtenerConfig(){

    String texto = "";

    try {
        BufferedReader bf = new BufferedReader(new FileReader("config.txt"));
        String temp = "";
        String bfRead;
        while((bfRead = bf.readLine()) != null)
            temp = temp + bfRead;

        texto = temp;
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
    return texto;
}

But when you run it, it tells me that it could not find the file.

If I put the full path ("C: \ Documents \ blabla \ config.txt") instead of ("config.txt") it works, but it does not work that way.

Here is a photo so they can see that they are both in the same place.

Thank you!

    
asked by Juan Manuel 06.12.2016 в 23:44
source

2 answers

1

You can use getResourceAsStream () and in your code it would look like this:

public String obtenerConfig(){

    String texto = "";
    String temp = "";

    try {

        InputStream in = this.getClass().getResourceAsStream("config.txt");
        Reader reader = new InputStreamReader(in, "utf-8");         
        BufferedReader bf = new BufferedReader(reader);


        String bfRead;
        while((bfRead = bf.readLine()) != null)
            temp = temp + bfRead;

        texto = temp;
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
    return texto;
}   
    
answered by 07.12.2016 / 00:25
source
1

What you need in this case is a class loader, which allows an application to access the repositories of available classes and resources. But not any class loader, because there are many in the execution environment, but the one that loaded the class Config .

❍ If you're using Java 5 / 6 , you can do the following:

public String obtenerConfig() {
    StringBuilder sb = new StringBuilder();
    try {
        InputStream inputStream = Config.class.getResourceAsStream("config.txt");
        InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader reader = new BufferedReader(streamReader);
        for (String line; (line = reader.readLine()) != null;) {
            sb.append(line);
            sb.append('\n');
        }
        reader.close();
        streamReader.close();
        inputStream.close();
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
    return sb.toString();
}

❍ If you're using Java 7 / 8 , you can do the following:

public String obtenerConfig() {
    String content = null;
    try {
        Path path = Paths.get(getClass().getResource("config.txt").toURI());
        byte[] bytes = Files.readAllBytes(path);
        content = new String(bytes, StandardCharsets.UTF_8);
    } catch (URISyntaxException | IOException e) {
        System.out.println("Error: " + e.getMessage());
    }
    return content;
}
    
answered by 07.12.2016 в 00:17