How to save a text from an external .txt file in a string variable?

0

Hello, I am very new to the Java world and I have a small problem, I currently want to save a text from a .txt file in a variable String, but with the little knowledge I have of Java I did something very rudimentary that also does not work well because reading the variable "text" which is where I store the .txt information shows me the following: "nullThis is a test".

(This is a test) is the content of the txt.

I hope that some wise person can help me thank you in advance.

public class Acceso_fichero {

    public static void main(String[] args) {

        Leer_fichero accediendo=new Leer_fichero();
        accediendo.lee();
    }
}

class Leer_fichero{

    String texto;

    public void lee() {

        try {
            FileReader entrada=new FileReader("C:/Users/Etchko/Desktop/leeme.txt");

                int c=0;

                while(c!=-1) {
                    c=entrada.read();

                    char letra=(char)c;

                    texto+=letra;
                }

                entrada.close();

                System.out.println(texto);

        } catch (IOException e) {

            System.out.println("No se ha encontrado el archivo");
        }
    }
}
    
asked by J.M.C 21.11.2018 в 15:37
source

2 answers

0

Initialize your text variable, because by default any uninitialized object assigns null Why? Because String is an object as it extends from java. lang.Object and when it is not initialized it assigns it null. It is not like the primitives, for example, in the case of int, but if you initialize it, you assign it a 0 . So that when you do the first text + = letter; contcatena null and E . Your code would look like this:

public class Acceso_fichero {

    public static void main(String[] args) {

        Leer_fichero accediendo=new Leer_fichero();
        accediendo.lee();
    }
}

class Leer_fichero{

    String texto="";

    public void lee() {

        try {
            FileReader entrada=new FileReader("C:/properties/leeme.txt");

                int c=0;

                while(c!=-1) {
                    c=entrada.read();

                    char letra=(char)c;

                    texto+=letra;
                }

                entrada.close();

                System.out.println(texto);

        } catch (IOException e) {

            System.out.println("No se ha encontrado el archivo");
        }
    }
}
    
answered by 21.11.2018 / 15:59
source
0

This class "FileReader " has methods that allow us to read characters.

"FileReader" does not contain methods that allow us to read complete lines (sometimes it is required),  but "BufferedReader" you can build a "BufferedReader" from "FileReader " as I show you here:

        /*Incio del fichero y creación de BufferedReader para poder
         ** lectura (disponer del metodo readLine())*/

        File archivo = new File("C:\Users\Etchko\Desktop\leeme.txt");
        FileReader entrada = new FileReader(archivo);
        BufferedReader br = new BufferedReader(entrada);

The way to read it would be:

while((texto=br.readLine())!=null)

Here you can do what you want within the cycle and the way to go through it is easier.

I share your code with some modifications with what I mentioned before, so you can prove it and see that it is better to implement it like this:

public class Acceso_fichero {

    public static void main(String[] args) {
        Leer_fichero.lee();
    }
}

class Leer_fichero{



    public static void lee() {
        String texto = "";
        try {
            /*Incio del fichero y creación de BufferedReader para poder
             ** lectura (disponer del metodo readLine())*/
            File archivo = new File("C:\Users\Etchko\Desktop\leeme.txt");
            FileReader entrada = new FileReader(archivo);
            BufferedReader br = new BufferedReader(entrada);

            /*Leer Fichero*/
         while((texto=br.readLine())!=null){
            System.out.println(texto);
         }
            entrada.close();

            } 
        catch (IOException e) {

            System.out.println("No se ha encontrado el archivo:" + e.getMessage());
            }
    }
}

Good luck ...

    
answered by 21.11.2018 в 16:34