使用Java重新排列文件数组内容

时间:2021-07-28 06:42:37

I have a File array where i have list of file names from a folder.

我有一个文件数组,其中我有一个文件夹中的文件名列表。

e.g.

例如

File file = new File("/path");
File[] arr = file.listFiles();

So my arr has all file names inside file path.

所以我的arr在文件路径中有所有文件名。

Say arr[folder1 , Folder2, Folder3].

说arr [folder1,Folder2,Folder3]。

Also i have a String array list which contains ( Folder3 , Folder1 , Folder2).

我还有一个String数组列表,其中包含(Folder3,Folder1,Folder2)。

I need to change the arr contents as per arrrayList order.

我需要根据arrrayList顺序更改arr内容。

(i.e) after processing my arr will have [ Folder3 , Folder1 , Folder2].

(即)处理后我的arr会有[Folder3,Folder1,Folder2]。

I need to do it because , i have to read folders based on certain hierarchy.

我需要这样做因为,我必须根据特定的层次结构读取文件夹。

am new to this please help how can i achieve it.

我是新手,请帮助我如何实现它。

2 个解决方案

#1


2  

If you already have the folder names as a String list in the desired order, why do you use File.listFiles()?

如果您已按所需顺序将文件夹名称作为字符串列表,为什么要使用File.listFiles()?

Just create the File objects from the names in the order they are listed, like this:

只需按照列出的顺序从名称中创建File对象,如下所示:

List< String > arrayList = Arrays.asList("Folder3", "Folder1", "Folder2");

File parent = new File("/path");
File[] arr = new File[arrayList.size()];

for ( int i = 0; i < arr.length; i++ )
    arr[i] = new File(parent, arrayList.get(i));

// Now arr will contain File objects in the order they are listed in arrayList

#2


-1  

First you need to convert your array to a list using the Arrays.asList method. Then you can sort your array list using Collections.sort(yourNewArrayList):

首先,您需要使用Arrays.asList方法将数组转换为列表。然后,您可以使用Collections.sort(yourNewArrayList)对数组列表进行排序:

List<File> sortedFileArrayList = Collections.sort(Arrays.asList(new File("/path").listFiles()));

However, if you only need your array sorted without being converted to a list (as list conversion is not necessary for sorting) you can use

但是,如果您只需要对数组进行排序而不将其转换为列表(因为列表转换不需要进行排序),您可以使用

Arrays.sort(new File("/path").listFiles());

instead.

代替。

#1


2  

If you already have the folder names as a String list in the desired order, why do you use File.listFiles()?

如果您已按所需顺序将文件夹名称作为字符串列表,为什么要使用File.listFiles()?

Just create the File objects from the names in the order they are listed, like this:

只需按照列出的顺序从名称中创建File对象,如下所示:

List< String > arrayList = Arrays.asList("Folder3", "Folder1", "Folder2");

File parent = new File("/path");
File[] arr = new File[arrayList.size()];

for ( int i = 0; i < arr.length; i++ )
    arr[i] = new File(parent, arrayList.get(i));

// Now arr will contain File objects in the order they are listed in arrayList

#2


-1  

First you need to convert your array to a list using the Arrays.asList method. Then you can sort your array list using Collections.sort(yourNewArrayList):

首先,您需要使用Arrays.asList方法将数组转换为列表。然后,您可以使用Collections.sort(yourNewArrayList)对数组列表进行排序:

List<File> sortedFileArrayList = Collections.sort(Arrays.asList(new File("/path").listFiles()));

However, if you only need your array sorted without being converted to a list (as list conversion is not necessary for sorting) you can use

但是,如果您只需要对数组进行排序而不将其转换为列表(因为列表转换不需要进行排序),您可以使用

Arrays.sort(new File("/path").listFiles());

instead.

代替。