首页 > 解决方案 > how to display the console result in a textarea with java

问题描述

here is a java program that allows to display the files of each directory the problem how to display the result in a textarea

private static void 
findFilesRecursively(File file, Collection<File> all, final String extension) {

        final File[] children = file.listFiles(new FileFilter() {
            public boolean accept(File f) {
                    return f.getName().endsWith(extension) ;
                }}
        );
        if (children != null) {
            //Pour chaque fichier recupere, on appelle a nouveau la methode
            for (File child : children) {
                all.add(child);
                findFilesRecursively(child, all, extension);
            }
        }
    }
    public static void main(String[] args)
    {
    //try {
        final Collection<File> all = new ArrayList<File>();
       // JFileChooser fc = new JFileChooser(".");
              //  int returnVal = fc.showOpenDialog(null);
        findFilesRecursively(new File("c:\\repertoire"), all,"");

        //File outputFile = new File("C:\\Users\\21365\\Desktop\\tt.txt");
        //FileOutputStream fos = new FileOutputStream(outputFile);
    for (int i = 0; i < all.size(); i++) {
        for (File file : all) {

              if(file.isDirectory()==true){
                  System.out.println("le repertoire \t"+file.getName()+"\t contien");
              }else{

标签: java

解决方案


您不应该两次遍历您的列表 - 摆脱这两个 for 循环之一:

   for (int i = 0; i < all.size(); i++) {
        for (File file : all) {

也不要使用System.out.println(…)打印到控制台,只需创建一个 JFrame / JTextArea 并使用它的append(String text)方法,例如:

if (file.isDirectory() == true) {
      yourTextArea.append("le repertoire \t" + file.getName() + "\t contien");
} else {
      yourTextArea.append(file.getName());
}

推荐阅读