Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning an Array incorrectly, overwriting all with the last item

I've seen some similar post but not the same issue I'm having. What I'm trying to do is calling a function that shows the name of the files numbered as

1- File1.txt,
2- File2.txt
..

This works as expected. The problem is that I need to return the path of those files in another Array. When I System.out.print(arrayRutasFicheros[j]) inside for, it shows all paths correctly. But when I try to access arrayRutasFicheros[j] from other functions. It just overwrites all the paths and just shows the last one.

public static String[] listarArchivos() throws IOException{

    File[] listadoDeFiles = documento.listFiles();
    File[] arrayFicheros = null;
    String[] arrayRutasFicheros = null;

    if(documento.exists() ) {

        for (int k=0; k< listadoDeFiles.length ;k++) {

            File ficheroRuta = listadoDeFiles[k];

            File fichero = new File(documento.getPath() + sep+ ficheroRuta.getName());

            if(fichero.isFile()==true) {

                arrayFicheros =new File[] {fichero};

                System.out.println( k + " - " + ficheroRuta.getName());

                for(int j= 0; j<arrayFicheros.length; j++) {

                    arrayRutasFicheros =  new String[] {arrayFicheros[j].getPath()};

                    //here it works and it display all the path
                    System.out.println(arrayRutasFicheros[j]);
                }   

            }   
        }       
    }

    return arrayRutasFicheros;
}

public static muestraUnArchivo() throws IOException {

    String [] Fichero =listarArchivos();

    for(int k=0; k<Fichero.length; k++) {

    //here just the last one
    System.out.print(Fichero[k]);       
    }
}

Expected:

-E:\Eclipse\Files\File1.txt   
-E:\Eclipse\Files\File2.txt  
-E:\Eclipse\Files\File3.txt   

Output:

-E:\Eclipse\Files\File3.txt
like image 349
Jhony Avatar asked Dec 07 '25 07:12

Jhony


1 Answers

Your array is recreated on every iteration of the loop, which is also on an array of length 1. You need a dynamic structure to store the string paths, because you don't know ahead of time how many there are. Also, you don't need to keep creating single element arrays; use a List<String>. Something like,

public static String[] listarArchivos() throws IOException {
    List<String> al = new ArrayList<>();
    if (documento.exists()) {
        File[] listadoDeFiles = documento.listFiles();
        for (File ficheroRuta : listadoDeFiles) {
            File fichero = new File(documento.getPath() 
                    + sep + ficheroRuta.getName());
            if (fichero.isFile()) {
                al.add(fichero.getPath());
            }
        }
    }

    return al.toArray(new String[0]);
}
like image 95
Elliott Frisch Avatar answered Dec 08 '25 21:12

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!