Reputation: 594
I am trying to read all files within a directory using java nio, i have the following directory structure
--dir1
|
-file1
-file2
-file3
Now i need to make sure that file3 gets read first and then followed by file2 and then file1, i.e i need to assign some priorities to the files and then read higher priority files first rather than the lower priority ones, how can i achieve this ?
Upvotes: 0
Views: 69
Reputation: 1377
You can use PriorityQueue
as i have implimented that in simple way hope this solves your problem
public class Test
{
public static void main(String[] args)
{
Comparator<String> comparator = new StringLengthComparator();
PriorityQueue<String> queue =
new PriorityQueue<String>(10, comparator);
queue.add("file3");
queue.add("file1");
queue.add("file2");
while (queue.size() != 0)
{
System.out.println(queue.remove());
}
}
}
Hear i am comparing your condition using comparator
import java.util.Comparator;
public class StringLengthComparator implements Comparator<String> {
@Override
public int compare(String x, String y) {
if (x.equalsIgnoreCase("file3")) {
return -1;
}
if (x.equalsIgnoreCase("file1")) {
return 1;
}
return 0;
}
}
Assign your priorities as you like and read it .
Resulting
file3
file2
file1
Upvotes: 1