java - 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{
解决方案
您不应该两次遍历您的列表 - 摆脱这两个 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());
}
推荐阅读
- flutter - 检查对象是否具有空值
- javascript - 在 JavaScript 中检测浏览器的在线状态
- web-scraping - 获取带有部分字符串 beautiful Soup 的特定标签
- python - 在主页中导入另一个模块
- python-3.x - 在比较熊猫中的两个数据帧时遇到麻烦
- hyperledger-fabric - Hyperledger Fabric 2.0-beta Javascript 开发模式
- java - 从 Firebase 为每个特定帖子检索特定数据的比 datasnapshot 更好的方法?返回的位置错误
- c# - 在 asp.net 核心应用程序中应该如何实现对数据库的异步日志记录?
- c++ - 要显示的 C 程序输入是 int 还是 char?
- reactjs - 错误:项目(...):渲染没有返回任何内容。这通常意味着缺少 return 语句。或者,不渲染任何内容,返回 null