Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

java - How to get contents of a folder and put into an ArrayList

I want to use

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

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){

    }
}
}

Thanks, obiedog

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The easiest way of doing that is:

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()));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...