Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 逐行读取文件夹中的所有.txt文件_Java_Multithreading_Csv_Bufferedreader_Filereader - Fatal编程技术网

Java 逐行读取文件夹中的所有.txt文件

Java 逐行读取文件夹中的所有.txt文件,java,multithreading,csv,bufferedreader,filereader,Java,Multithreading,Csv,Bufferedreader,Filereader,现在我正在处理一个线程,它不断地在文件夹中查找.txt文件。一旦找到一个文件,它应该逐行读取内容(使用缓冲读取器和拆分器(分隔符)最终将这些数据插入数据库) 事情是在我的代码查找具有特定名称的特定文件之前,它工作得很好,但后来我的教授告诉我,程序需要查找文件夹中的任何.txt文件。因此,我添加了一些代码来实现这一点,并试图使它与我的旧代码一起工作 我认为不起作用的是以下几行: FileReader fr=新的FileReader(fileList[i]) BufferedReader b=新的

现在我正在处理一个线程,它不断地在文件夹中查找.txt文件。一旦找到一个文件,它应该逐行读取内容(使用缓冲读取器和拆分器(分隔符)最终将这些数据插入数据库)

事情是在我的代码查找具有特定名称的特定文件之前,它工作得很好,但后来我的教授告诉我,程序需要查找文件夹中的任何.txt文件。因此,我添加了一些代码来实现这一点,并试图使它与我的旧代码一起工作

我认为不起作用的是以下几行:

  • FileReader fr=新的FileReader(fileList[i])
  • BufferedReader b=新的BufferedReader(fr)
我得到的错误是未找到捕获文件异常e

好的,这是我的密码D

public void run() {

    while (true) {
        try {
            Thread.sleep(20000);

            boolean flag = false;

            try {

                FilenameFilter filter = new FilenameFilter() {
                    public boolean accept(File dir, String fileName) {
                        return fileName.endsWith("txt");
                    }
                };

                File f = new File("D:\\Mis Documentos\\");
                String[] fileList = f.list(filter);
                for (int i = 0; i < fileList.length; i++) {

                     System.out.println(fileList[i]);
                    FileReader fr = new FileReader(fileList[i]);
                    BufferedReader b = new BufferedReader(fr);
                    String cadena = b.readLine();
                    Validaciones v = new Validaciones();

                    if (cadena == null) {

                        JOptionPane.showMessageDialog(null, "El fichero está vacío");

                    } else {

                        while (cadena != null) {

                            String[] fields = cadena.split(SEPARATOR);

                            EntidadDAO ed = new EntidadDAODB();
                            Entidad ent = new Contacto();

                            if (fields.length == 7) {

                                // System.out.println("fields es 7");
                                for (int i2 = 0; i2 < fields.length; i2++) {

                                    nombre = fields[0];
                                    apellido = fields[1];
                                    alias = fields[2];
                                    direccion = fields[3];
                                    telefono = fields[4];
                                    nacimiento = fields[5];
                                    email = fields[6];

                                }

                                if (v.validarThread(nacimiento, email, telefono) == true) {

                                    System.out.println("El contacto " + nombre + " se validó correctamente");
                                } else {

                                    System.out.println("No se pudo cargar el contacto " + nombre + ". Error al validar los datos.");
                                    cadena = b.readLine();
                                    continue;

                                }

                            ((Contacto) ent).setNombre(nombre);
                            ((Contacto) ent).setApellido(apellido);
                            ((Contacto) ent).setAlias(alias);
                            ((Contacto) ent).setDireccion(direccion);
                            ((Contacto) ent).setTelefono(telefono);
                            ((Contacto) ent).setNacimiento(nacimiento);
                            ((Contacto) ent).setEmail(email);
                            ((Contacto) ent).setFavorito("no");
                            // System.out.println(ent.toString());
                            //  ed.agregarEntidad(ent);
                            cadena = b.readLine();

                            } else {

                                System.out.println("No se pudo cargar el contacto " + nombre + ". Faltan datos.");
                                cadena = b.readLine();
                            }

                        }
                    }
                    b.close();
                    //      moverFichero();
                } // aca termina el for que lee cada archivo txt.

            } catch (FileNotFoundException e) {

                System.out.println("No se encontró el fichero");

            } catch (Exception e) {

                System.out.println("Ocurrió un error al leer o cerrar el fichero" + e);

            }

        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.out.println("Error al ejecutar thread");
        }
    }
}
public void run(){
while(true){
试一试{
睡眠(20000);
布尔标志=假;
试一试{
FilenameFilter筛选器=新建FilenameFilter(){
公共布尔接受(文件目录,字符串文件名){
返回fileName.endsWith(“txt”);
}
};
文件f=新文件(“D:\\Mis Documentos\\”;
字符串[]fileList=f.list(过滤器);
for(int i=0;i
问题是您只传递
文件阅读器的文件名

FileReader fr = new FileReader(fileList[i]);
给出该文件的完整路径

FileReader fr = new FileReader("D:\\Mis Documentos\\"+fileList[i]);

我没有我的电脑与我一起解决你的代码在目前;但是,请尝试更改行字符串cadena=b.readLine();as String=“”;然后在第行while(cadena!=null){,请尝试将其更改为while(String cadena=b.readline())!=null){。看起来您有很多cadena=b.readline();在您的代码中,可以删除。您是否尝试在您认为错误的位置添加断点并单步执行代码?非常感谢!这非常有效!现在我可以继续编写代码了。谢谢!!