将文件名添加到java中的数组列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2532953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:55:51  来源:igfitidea点击:

Adding file names to an array list in java

javafile

提问by Chris G

I am currently needing to load the contents of a folders filenames to an arraylist I have but I am unsure as how to do this.

我目前需要将文件夹文件名的内容加载到我拥有的数组列表中,但我不确定如何执行此操作。

To put it into perspective I have a folder with One.txt, Two.txt, Three.txt etc. I want to be able to load this list into an arraylist so that if I was to check the arraylist its contents would be :

为了正确看待它,我有一个包含 One.txt、Two.txt、Three.txt 等的文件夹。我希望能够将这个列表加载到一个数组列表中,这样如果我要检查数组列表,它的内容将是:

arraylist[0] = One

数组列表[0] = 一

arraylist[1] = Two

数组列表[1] = 2

arraylist[3] = Three

数组列表[3] = 三

If anyone could give me any insight into this it would be much appreciated.

如果有人能让我对此有任何见解,将不胜感激。

回答by Hyman

You can try with

你可以试试

File folder = new File("myfolder");

if (folder.isDirectory())
{
  // you can get all the names
  String[] fileNames = folder.list();

  // you can get directly files objects
  File[] files = folder.listFiles();
}

回答by Ziv

See the Jave File API, particularly the list()function.

请参阅 Java 文件 API,尤其是list()函数。

File my_dir = new File("DirectoryName");
assert(my_dir.exists()); // the directory exists
assert(my_dir.isDirectory()); // and is actually a directory

String[] filenames_in_dir = my_dir.list();

Now filenames_in_diris an array of all the filenames, which is almost precisely what you want.

现在filenames_in_dir是所有文件名的数组,这几乎正是您想要的。

If you want to strip the extensions off the individual filenames, that's basic string handling - iterate over the filenames and take care of each one.

如果您想从单个文件名中去除扩展名,那就是基本的字符串处理 - 遍历文件名并处理每个文件名。

回答by cletus

File dir = new File("/some/path/name");
List<String> list = new ArrayList<String>();
if (dir.isDirectory()) {
  String[] files = dir.list();
  Pattern p = Pattern.compile("^(.*?)\.txt$");
  for (String file : files) {
    Matcher m = p.matcher(file);
    if (m.matches()) {
      list.add(m.group(1));
    }
  }
}

回答by kingpin

Have a look at java.io.File it gives you all the control you may need. Here is a URL for it http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html

看看 java.io.File 它为您提供了您可能需要的所有控制。这是它的 URL http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html

回答by polygenelubricants

Here's a solution that uses java.io.File.list(FilenameFilter). It keeps the .txtsuffix; you can strip these easily if you really need to.

这是一个使用java.io.File.list(FilenameFilter). 它保留.txt后缀;如果你真的需要,你可以很容易地剥离这些。

File dir = new File(".");
List<String> list = Arrays.asList(dir.list(
   new FilenameFilter() {
      @Override public boolean accept(File dir, String name) {
         return name.endsWith(".txt");
      }
   }
));
System.out.println(list);

回答by Rose Blax

Here's My answer, I've used this before personally to get all the filenames,
to be used in a loadsave function in one of my games.

这是我的答案,我之前亲自使用过它来获取所有文件名,
以便在我的一款游戏的加载保存功能中使用。

  public void getFiles(String path){
            //Put filenames in arraylist<string>
        File dir = new File(path);
        ArrayList<String> filenames = new ArrayList<String>();
        for(File file : dir.listFiles()){
            savefiles.add(file.getName());
        }
            //Check if the files are in the arraylist
        for (int i = 0; i < savefiles.size(); i++){
            String s = savefiles.get(i);
            System.out.println("File "+i+" : "+s);
        }
        System.out.println("\n");
    }


I hope that may have been of use to you C: - Hugs rose


我希望这可能对你有用 C: - 拥抱玫瑰