Reputation: 41
I am trying to import a CSV file online which contains 6 fields. I am trying to import from a web link. I am able to download the file and use the file name as well. What I am trying to do is to read the data from the file and output it to my program. So far I have this:
public class Element {
public static final String CSV_FILE_URL = "http://www.google.com/File.csv"; //Actual URl not published
public static void main(String[] args) throws IOException {
URL url = new URL(CSV_FILE_URL);
Scanner input =
new Scanner(url.openConnection().getInputStream());}
int number ;
String symbol;
String name;
int group;
int period;
double weight;
Upvotes: 0
Views: 1102
Reputation: 13858
It might be a good idea to not use a Scanner but read the file line based, transform the lines into elements and then work on these like this:
public static void main(String[] args) {
try {
URL url = new URL("http://www.google.com/File.csv");
try(InputStream in = url.openStream();
InputStreamReader inr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(inr)) {
String line = br.readLine();
while(line != null) {
//Best case: no String separation, no ; contained in data items. If not
//you need some other way to split this
String[] elements = line.split(";");
//Deal with that one line
//Get next line
line = br.readLine();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 3