遍历目录,读入文件,并将它们打印成html文件

时间:2012-07-10 19:31:58

标签: java file-io iteration bufferedreader filewriter

我正在尝试从JfileChooser中选择一个目录,然后遍历该目录读取所有文件并将它们全部写入一个大的HTML文件以便于查看。以下是应该完成此操作的代码片段:

else if(arg0.getSource()==generate){
        //Create HTML report in same directory that file(s) came from
        try{

            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnval = fileChooser.showSaveDialog(this);
            if(returnval == JFileChooser.APPROVE_OPTION){
                File fileName = new File( "./report.html" );
                BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
                bw.write("<html>");
                bw.write("<body>");
                bw.write("<h1>Graded Assignment</h1>");
                bw.write("<p>");
                //for loop here that appends all the files in a directory
                File directory = new File(fileChooser.getSelectedFile().getAbsolutePath());
                for( File f : directory.listFiles() ){
                    FileInputStream fstream = new FileInputStream(f);
                    DataInputStream in = new DataInputStream(fstream);
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    bw.write(br.readLine() + "\n");
                }
                bw.write("</p>");
                bw.write("</body>");
                bw.write("</html>");

                bw.close();
                Desktop.getDesktop().browse(fileName.toURI());
            }
        }
        catch(FileNotFoundException fnf){
            fnf.printStackTrace();
        }
        catch(IOException io){
            io.printStackTrace();
        }

它将进入for循环,但不会在第一个文件中读取。我得到某种AWT-EventQueue错误。任何人都可以重新创建并告诉我问题可能是什么?感谢。

1 个答案:

答案 0 :(得分:2)

在评论中,您应该检查它是否是目录并采取适当的行动:

File directory = new File(fileChooser.getSelectedFile().getAbsolutePath());
  getFiles(directory);//calls method to get all the files in the directory
  ...
  void getFiles(File directory) {
            for( File f : directory.listFiles() ){
                if(!f.isDirectory()) {//is a file lets read it
                    FileInputStream fstream = new FileInputStream(f);
                    DataInputStream in = new DataInputStream(fstream);
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    bw.write(br.readLine() + "\n");
               }else {//wil make it a recursive search
                   getFiles(f);
               }
          }
    }

虽然我给出的方法也进行了递归文件搜索,如果给定目录中有一个目录,它将遍历所有目录获取所有文件

<强>附录:

你应该使用:chooser.showOpenDialog()而不是chooser.showSaveDialog(),例如:

   JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int returnVal = chooser.showOpenDialog(parent);//this must be changed
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this file: " +
            chooser.getAbsolutePath());
    }

<强>参考文献: