Insert data into 2d array from file via split?
How could one populate a 2d array from a text file using split?
String proxies[][] = {{"127.0.0.1","80"}, {"127.0.0.1","443"}, {"127.0.0.1","3306"}};
In my text file I have data with a ip:port on each line:
127.0.0.1:80
127.0.0.1.443
127.0.0.1.330开发者_如何学JAVA6
I could populate a 1d array using split like this:
proxies = everyLine.split("\\n");
How would I insert the ip:port data into a 2d array?
String[] lines = everyLine.split("\\n");
String[][] proxies = new String[lines.length][];
int i=0;
for ( String line : lines )
{
proxies[i++] = line.split(":");
}
Using Java constructs it's not possible. You can use Apache Commons method FileUtils#lineIterator(File, String)
to iterate over lines and apply String.split(String)
on each
you could split on the :
operator.
String []proxies = everyLine.split("\\n");
for(int i=0;i<proxies.length;i++){
String[] anotherDimention= proxies[i].split(":");
// do something useful with it
}
精彩评论