Salut James!
Voilà le code.
Libre à toi de l'adapter , de l'utiliser et même de le distribuer.
Si tu y apportes des améliorations, pourrais-tu publier ton code ici même?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.StringTokenizer;
/**
* This class is to be used to directly import data from a CSV file available on
* an URL. Only http:// protocol is supported ! (not file://)
*
* @author Philippe FERY
* @version 1.0
*
* Created on Jan 24, 2007
*/
public class CSVImporter {
private String csvFileURL;
private ArrayList<ArrayList> dataTable;
private StringTokenizer splitter;
/**
* Create a new CSVImporter.
*
* @param csvFileURL
* The file URL
*/
public CSVImporter(String csvFileURL) {
super();
this.csvFileURL = csvFileURL;
dataTable = new ArrayList<ArrayList>();
}
/**
* Import data from the file at specified URL
*
* @param csvFileURL
* The file URL
* @throws IOException
* If problem occurs during rad of file
*/
public void importCSVFile() throws IOException {
URL url = new URL(csvFileURL);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.connect();
InputStreamReader isr = new InputStreamReader(httpConnection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
String line = "";
ArrayList<String> dataLine;
while ((line = reader.readLine()) != null) {
dataLine = new ArrayList<String>();
splitter = new StringTokenizer(line, ",");
while (splitter.hasMoreElements()) {
String data = (String) splitter.nextElement();
dataLine.add(data);
dataTable.add(dataLine);
}
}
}
/**
* Return a ArrayList of ArrayList with all data in String format
*
* @return The data read from URL
*/
public ArrayList<ArrayList> getDataList() {
return dataTable;
}
/**
* Return a String[][] double array of data
*
* @return The data read form URL
*/
public String[][] getData() {
if (dataTable.size() == 0)
return null;
int rowCount = dataTable.size();
int colCount = ((ArrayList) dataTable.get(0)).size();
String[][] dataArray = new String[rowCount][colCount];
ArrayList dataLine;
String data;
for (int r = 0; r < rowCount; r++) {
dataLine = (ArrayList) (dataTable.get(r));
for (int c = 0; c < dataLine.size(); c++) {
data = (String) (dataLine.get(c));
dataArray[r][c] = data;
}
}
return dataArray;
}
/**
* Use the main method below to test CSV import.
*
* @param args
* The file URL starting with http://
*/
public static void main(String[] args) {
if (args == null || args.length == 0) {
System.out.println("Syntax: CSVImporter [httpUrlString]");
return;
} else if (!args[0].startsWith("http://")) {
System.out.println("Note: only HTTP protocol is supported. Please specify an URL beginning with http://");
return;
}
try {
CSVImporter importer = new CSVImporter(args[0]);
importer.importCSVFile();
String[][] data = importer.getData();
StringBuffer buffer;
for (int i = 0; i < data.length; i++) {
buffer = new StringBuffer();
for (int j = 0; j < data[0].length; j++) {
buffer.append(data[i][j]).append("\t");
}
System.out.println(buffer.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
;-)
HackTrack