简体   繁体   中英

iterate through directory, read in files, and print them out into an html file

I am trying to choose a directory from a JfileChooser and then iterate through that directory reading in all the files and the writing them all out into one big HTML file for easy viewing. Here is the snippet of code that should accomplish this:

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();
        }

It will get to the for loop and but not read in the first file. I get some kind of AWT-EventQueue error. Can anyone recreate this and tell me what the problem might be? Thanks.

As in the comment you should check if it is a directory or not and act appropriately:

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);
               }
          }
    }

take not though the method I gave also does recursive file searching, if there is a directory within the given directory it will traverse through all its directories getting all the files

Addendum:

you shoud use: chooser.showOpenDialog() and not chooser.showSaveDialog() see here for example:

   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());
    }

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM