Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String[] to File[] 2

Status
Not open for further replies.

aswolff

Programmer
Jul 31, 2006
100
US
Hi,

I have a property variable which contains a pipe seperated String of file names. I want to take that list of file names and create an array of File objects.

My code is

Code:
String xsls = properties.getProperty("xsls");
File [] xslfiles = xsls.split("\\|");
[code]

I get the following error:

WebApplicant.java:43: incompatible types
found   : java.lang.String[]
required: java.io.File[]
                File [] xslfiles = xsls.split("\\|");
                                             ^
I understand the error...split returns a string[] and I am trying to stuff it into a File[].  But how can I solve?

Thanks.
 
My solution was this..which is OK i guess...still curious if anybody has another way of accomplishing the same thing without a loop.

Thanks.

Code:
String [] xslArr = xsls.split("\\|");
File [] xslfiles = new File[xslArr.length];
for (int i = 0;i < xslArr.length; i++)
{
xslfiles[i] = new File(xslArr[i]);
System.out.println(xslArr[i] + "->" + xslfiles[i].getName());
}
 
You'll probably need to iterate through the String array. something like
Code:
String[] s = xsls.split("\\|");
for (int i = 0; i <  s.length; i++){
 xslfiles[i] = new File(s[i]);
}

-----------------------------------------
I cannot be bought. Find leasing information at
 
Just a suggestion: maybe you don't need the File array, and you can use the String array deeper in the code and instantiate File objects just when you need to use it.

Cheers,
Dian
 
I agree with Dian. Although your code is 100% correct it could be a better idea to create the File objects when they are really needed.


Example:

String[] files = xsls.split("\\|");

for (String s : files){
doSomethingWithTheFile(new File(s));
}


Tom.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top