I want to read every Airport and corresponding airport code from each line of this airport file, and then split the line by [,] so i can put desired info into an database.
How can i achieve this?
I want to read every Airport and corresponding airport code from each line of this airport file, and then split the line by [,] so i can put desired info into an database.
How can i achieve this?
LineNumberReader lnr = new LineNumberReader( new InputStreamReader(new URL( "http://..." ).openStream()) );
String readLine = lnr.readLine();
do {
String[] split = readLine.split(",");
for( String col : split )
{
// Write to DB
System.out.println(col);
}
readLine = lnr.readLine();
} while (readLine!=null)
You may want to use a CSV reading library, such as OpenCSV
You can use Apache Commons CSV to read the file. Take a look at this post CSV parsing in Java - working example..?.
I have written a small sample in ideone to show you how to read line by line from a given stream and then split the line by a comma:
Here is the code:
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
String temp;
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()) {
temp = sc.nextLine();
String[] strs = temp.split(",");
for (int i = 0; i < strs.length; ++i) {
System.out.println(strs[i]);
}
System.out.println("===========================================");
}
}
}
I do not solve the whole problem intentionally so that there is some thinking left for you as well. Also I have intentionally kept the sample as simple as possible so that it is easier to understand.
You can see an example in ideone here.