首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >遍历目录,读入文件,并将它们打印到html文件中

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

Stack Overflow用户
提问于 2012-07-11 03:31:58
回答 1查看 2.1K关注 0票数 0

我尝试从JfileChooser中选择一个目录,然后遍历该目录,读取所有文件,并将它们全部写出到一个大的HTML文件中,以便查看。下面是应该实现这一点的代码片段:

代码语言:javascript
复制
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错误。有没有人能重现这段视频,告诉我问题出在哪里?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-11 05:04:51

在注释中,你应该检查它是否是一个目录,并采取适当的行动:

代码语言:javascript
复制
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(),例如:

代码语言:javascript
复制
   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());
    }

参考:

  • http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JFileChooser.html
  • http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11420547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档