like reading a txt file in java and passing it to an array

1

I have the following code in java to read a txt file

                      // recibe la dirección del archivo
public String leerTxt (String direccion){ 
      String texto="";
      try {
        BufferedReader bf= new BufferedReader(new FileReader(direccion));
        String temp="";
        String bfRead;
        while ((bfRead = bf.readLine()) != null){ // se hace el ciclo, mientras bfRead tenga datos
          temp=temp+bfRead;// guardado del texto de archivo
        }
        texto= temp;
        }
      catch (Exception e){
        System.out.println("no se encontro archivo");
        }
      return texto;
}

my question is: how could I make an array with each of the characters of the txt, so if for example I have a file with a hello world, the h is in the 0,0 position of the matrix, the e in position 0.1, etc.

    
asked by Eduard Damiam 05.05.2017 в 00:35
source

1 answer

3

There are several ways, one of them may be with cycles:

char[] mat=new char[texto.lenght]
for(int i=0;i<texto.lenght;i++){
   mat[i]=texto.substring(i,i+1);
}
    
answered by 05.05.2017 / 00:59
source