Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - 获取文件夹中所有文件的文件名[重复](Getting the filenames of all files in a folder [duplicate])

Possible Duplicate:

(可能重复:)
Read all files in a folder

(读取文件夹中的所有文件)

I need to create a list with all names of the files in a folder.

(我需要创建一个列表,其中包含文件夹中所有文件的名称。)

For example, if I have:

(例如,如果我有:)

000.jpg
012.jpg
013.jpg

I want to store them in a ArrayList with [000,012,013] as values.

(我想将它们存储在一个带有[000,012,013]值的ArrayList 。)

What's the best way to do it in Java ?

(在Java中最好的方法是什么?)

PS: I'm on Mac OS X

(PS:我在Mac OS X上)

  ask by user680406 translate from so

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

1 Reply

0 votes
by (71.8m points)

You could do it like that:

(你可以这样做:)

File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
  if (listOfFiles[i].isFile()) {
    System.out.println("File " + listOfFiles[i].getName());
  } else if (listOfFiles[i].isDirectory()) {
    System.out.println("Directory " + listOfFiles[i].getName());
  }
}

Do you want to only get JPEG files or all files?

(你想只获得JPEG文件或所有文件吗?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...