Reputation: 51
public boolean importData(TransferSupport ts) {
try {
String dir = ts.getTransferable().getTransferData(DataFlavor.javaFileListFlavor).toString();
dir = dir.substring(1, dir.length()- 1);
System.out.println(dir);
DecimalFormat df = new DecimalFormat("#.#");
df.setRoundingMode(RoundingMode.CEILING);
//parsing directory names and converting them into file array
int lastComma = 0;
for(int i = 2; i < dir.length(); i++) {
//System.out.println(dir.substring(i-2,i));
if(dir.substring(i-2,i).equals(", ")) {
File file = new File(dir.substring(lastComma,i-2));
double num = file.length();
System.out.println(df.format((num/1024)/1024)+"MB");
lastComma = i;
}
}
}
I am implementing a method which takes in list of files by parsing through directory paths. String dir will be contain a directory of files. It will depends on how many files user drag and drop onto the program. Since I am storing info of files into table format, I have to store them into array format or some other data structure. Is there any advise?
most important question is, should I implement it by using File[] array? or List? I know that implementing it using File[] array will get me to count the number of files user drags and drops.
Upvotes: 0
Views: 45
Reputation: 718906
A List
is more flexible in general. However, in this case you can replace that messy string splitting code with this:
String[] files = dir.split(", ");
which also deals with one of the reasons for using a List
. And if you wanted to use a List
anyway:
List<String> files = Arrays.asList(dir.split(", "));
Note that this is a List<String>
rather than a List<File>
, but that shouldn't matter.
Upvotes: 1
Reputation: 846
List is more flexible, array is more constrained. If you have no specific reason to do otherwise, List is the go-first option.
Upvotes: 0