英文:
How to get file list in array using java?
问题
我正在尝试从目录中获取文件名并作为数组。
我希望:
List<String> list = ["c:/MyFolder/file1.txt", "c:/MyFolder/file2.txt"];
然后我可以这样获取它们:
println list[0] //c:/MyFolder/file1.txt
println list[1] //c:/MyFolder/file2.txt
我如何从这段代码中获得文件名数组?
import java.io.File;
public class FileListFromFolder {
public static void main(String a[]){
File file = new File("C:/MyFolder/");
String[] fileList = file.list();
for(String name:fileList){
System.out.println(name);
}
}
}
谢谢
英文:
i`m trying to get file name from directory as array.
i want:
List<String> list = ["c:/MyFolder/file1.txt", "c:/MyFolder/file2.txt"];
Then i can get then like:
println list[0] //c:/MyFolder/file1.txt
println list[1] //c:/MyFolder/file2.txt
how can i have filenames is array from this code?
import java.io.File;
public class FileListFromFolder {
public static void main(String a[]){
File file = new File("C:/MyFolder/");
String[] fileList = file.list();
for(String name:fileList){
System.out.println(name);
}
}
}
Thank you
答案1
得分: 0
以下是您需要的翻译:
这可能会对您有所帮助
public class FileListFromFolder {
public static void main(String a[]) {
File file = new File("C:/MyFolder/");
String[] fileList = file.list();
List<String> arrayToList = Arrays.asList(fileList);
System.out.println(arrayToList);
}
}
英文:
This might help you
public class FileListFromFolder {
public static void main(String a[]) {
File file = new File("C:/MyFolder/");
String[] fileList = file.list();
List<String> arrayToList = Arrays.asList(fileList);
System.out.println(arrayToList);
}
}
答案2
得分: 0
文件 file = new 文件("C:/我的文件夹/");
文件[] 文件列表 = file.获取文件列表();
对于 (文件 f : 文件列表) {
System.out.println(f.获取名称());
}
英文:
File file = new File("C:/MyFolder/");
File[] fileList = file.listFiles();
for(File f:fileList){
System.out.println(f.getName());
}
This is the way to iterate over a list of files in directory and to print their names.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论