Teacher provided me with a code to join all data from a file into a .csv file. it should work, but keeps throwing me a
"Cannot read the array length because "" is null" exception. here:
for (File ap : directorio.listFiles())
i know this has been answered in other threads, but cant find the error here, and teacher isnt available anymore. The ide is also giving me a small warning about the first if, telling me it is not necessary, but i dont think it is related.
if (directorio == null)
The full code is shown here:
public class JuntaDatosAP {
static String dirBase = "G:\\DATA\\data.2018000\\DATA.2018000";
static String archivoSalida = "G:\\DATA\\data2018000.csv";
static int numRegistros = 0;
public static void main(String[] args) {
File directorio = new File(dirBase);
if (directorio == null) {
System.out.println("Directorio base no existe.");
System.out.println(dirBase);
System.exit(1);
}
try(FileOutputStream fos = new FileOutputStream(archivoSalida, true)) {
PrintWriter pw = new PrintWriter(fos);
//Exception throwed down here
for (File ap : directorio.listFiles()) {
if (ap.isDirectory() && ap.getName().split("_").length != 6) {
System.out.println("Directorio no es un AP");
System.out.println(ap.getName());
} else {
System.out.println("Procesando " + ap.getName());
procesaAP(ap, pw);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(JuntaDatosAP.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JuntaDatosAP.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void procesaAP(File ap, PrintWriter pw) {
String linea;
File dirs = new File(ap.getAbsolutePath());
File[] dirsAux = dirs.listFiles();
for (File dirs2 : dirsAux) {
System.out.println(dirs2.getName());
String fecha[] = dirs2.getName().split("_");
fecha = fecha[6].split("T");
String dia = fecha[0];
String hora = fecha[1];
File archs = new File(dirs2.getAbsolutePath());
File[] archsAux = archs.listFiles();
for (File arch : archsAux) {
BufferedReader br;
try {
String datos[];
br = new BufferedReader(new FileReader(arch.getAbsolutePath()));
while ((linea = br.readLine()) != null) {
numRegistros++;
datos = linea.split(",");
// Simplemente escribo la nueva línea en el archivo, separada por comas
pw.println(ap.getName() + "," + linea);
}
br.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(JuntaDatosAP.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JuntaDatosAP.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
System.out.println("Total registros: " + numRegistros);
System.out.println("");
}
}