j'ai trouvé une classe de HackTrack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| package cj08;
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();
}
}
} |
Mais quand je teste :
1 2 3 4 5
| public void getDataFromCVS() {
CSVImporter cvs = new CSVImporter("http://ichart.finance.yahoo.com/table.csv?s=GOLD&a=06&b=31&c=2002&d=04&e=17&f=2008&g=d&ignore=.csv");
String[][] valeur = cvs.getData();
System.out.println("DATA CVS "+valeur) ;
} |
cela me renvoie "null"
je ne comprends pas trop comment utiliser cette classe
en fait j'aimerai récupérer pour chaque colonne un tableau comme ceci
private float[] valeur = {219, 222, 172, 174, 159, 28, 37, 71, 156, etc};
Partager