0

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?

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
Zentix
  • 13
  • 1
  • 3
  • 2
    http://www.whathaveyoutried.com? – mtk Dec 11 '12 at 15:38
  • I downloaded the URL to an TXT file and then i read the whole file in as a string and tried so split it. But the content was to big i think. – Zentix Dec 11 '12 at 15:52

3 Answers3

1
  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

mhaller
  • 14,122
  • 1
  • 42
  • 61
0

You can use Apache Commons CSV to read the file. Take a look at this post CSV parsing in Java - working example..?.

Community
  • 1
  • 1
izilotti
  • 4,757
  • 1
  • 48
  • 55
0

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.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176