how to send a String to a method?

0

Hello, I started to see java in the university and we are looking at files and they gave us to try a method to which we only have to send a String that will be the name with which the file will be saved if it is easy but Mark me an error, the method is as follows

public static String [] cargarArreglo (String archivo) throws IOException {

    // primero se lee el archivo para contar el número de líneas

         FileReader a = new FileReader (archivo);

         BufferedReader dentro = new BufferedReader (a);

         int n = 0;

         String linea = dentro.readLine ();

         while (linea != null) {

               linea = dentro.readLine ();

               n++;

          }
         a.close ();

        // Creación del arreglo

        String [] v = new String [n];

        // Ciclo para leer las cadenas del archivo en el arreglo

        a = new FileReader (archivo);

        dentro = new BufferedReader (a);

        int i = 0;

        linea = dentro.readLine ();

        while ((linea != null) && (i <n)) {

           v [i] = linea;

           linea = dentro.readLine ();

           i++;

         }

         a.close ();

         return v;

      }

That's the method, so what I did was to do the main in the same class since the method is static and I did this

public static void main(String[] args) throws IOException {

    String archivo1 = JOptionPane.showInputDialog("ingresa el nombre del archivo");
    cargarArreglo(archivo1);
}

but when I run it I get this error

Exception in thread "main" java.io.FileNotFoundException: archivo (El sistema no puede encontrar el archivo especificado)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at nombrearchivo.cargarArreglo(nombrearchivo.java:14)
at nombrearchivo.main(nombrearchivo.java:64)

In the Exception in thread part "main" java.io.FileNotFoundException: file (The system can not find the specified file) "file" is the string that is saved in the variable and then sent to the method

How can I fix it?

    
asked by lalo hernandez 07.04.2018 в 03:36
source

1 answer

2

The problem you have is not because you are incorrectly passing the String to the cargarArreglo() method, but, that String must contain the path (eg in Windows: D: \ folderX \ file.txt) file you want to upload, and not just the name (eg file.txt). For this reason, the file is not found and the exception is thrown.

To solve it I recommend you use the JFileChooser class that allows you to show a file selection window. So you avoid that kind of problems.

JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
    cargarArreglo(chooser.getSelectedFile().getAbsolutePath());
}
    
answered by 07.04.2018 / 03:50
source