如何获取文件夹的内容并放入ArrayList [英] How to get contents of a folder and put into an ArrayList

查看:144
本文介绍了如何获取文件夹的内容并放入ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用

File f = new File("C:\\");

使用该文件夹的内容制作一个ArrayList。

to make an ArrayList with the contents of the folder.

我对缓冲读者不是很好,所以请告诉我这是否更好。

I am not very good with buffered readers, so please tell me if that is better.

这是我到目前为止的代码:

Here's the code I have so far:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


public class buffered_read {
public static void main(String[] args) {
    File f = new File("C:\\");
    int x = 0;
    boolean b = true;
    File list[];
    while(b = true){

    }
}
}

谢谢,
obiedog

Thanks, obiedog

推荐答案

最简单的方法是:

File f = new File("C:\\");
ArrayList<File> files = new ArrayList<File>(Arrays.asList(f.listFiles()));

如果你想要的是一个名单:

And if what you want is a list of names:

File f = new File("C:\\");
ArrayList<String> names = new ArrayList<String>(Arrays.asList(f.list()));

这篇关于如何获取文件夹的内容并放入ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆