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?