如何使用java在文本区域中显示控制台结果



这里有一个java程序,它允许显示每个目录的文件如何在文本区域中显示结果的问题

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{

您应该不要在列表中迭代两次-去掉以下两个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());
}

最新更新