KioskeaKioskeaCommentCaMarcheInscrivez-vous, c'est gratuit !
Samedi 17 mai 2008 - 02:51:32

[java] télécharger un fichier csv

Rechercher : dans
[java] télécharger un fichier csv
par james2606
 Fil de Discussions
Statut : Non résolu
mercredi 24 janvier 2007 à 00:59:42
bonjour,
j'aimerai savoir si quelqu'un peut m'aider à coder un programme qui télécharge automatiquement un fichier csv depuis une adresse internet.
merci
Configuration: Windows XP
Firefox 2.0.0.1
Répondre à james2606  Signaler ce message aux modérateurs Aller au dernier message

1


  • Ce message vous semble utile, votez !
  • Signaler ce message aux modérateurs
Par HackTrack, le mercredi 24 janvier 2007 à 15:47:51 Fil de Discussions
Salut!

Dans quel cadre veux-tu faire cela? Un TP? Pour importer tes extraits de compte bancaire?

;-)
Répondre à HackTrack

2


  • Ce message vous semble utile, votez !
  • Signaler ce message aux modérateurs
Par james2606, le mercredi 24 janvier 2007 à 18:18:52 Fil de Discussions
nan en fait j'aimerai recuperer tous les fichiers csv sur les cours des actions sur yahoo finance .
tu peux m'aider, voila mon msn si jamais.
roussel_jeanchristophe@hotmail.com
Répondre à james2606

3


  • Ce message vous semble utile, votez !
  • Signaler ce message aux modérateurs
Par HackTrack, le jeudi 25 janvier 2007 à 11:36:11 Fil de Discussions 
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
Répondre à HackTrack
Discussions pertinentes trouvées dans le forum
02/12 18h11Fichier csv, java.Programmation02/12 18h221
23/01 19h00PHP : téléchargement d'un fichier CSVWebmastering23/01 19h000
16/01 16h04JAVA fichier CSV import/packageProgrammation16/01 16h040
03/05 16h05[Java] Ouvrir un fichier CSVProgrammation05/06 15h252
Plus de discussions sur « [java] télécharger un fichier csv » Discussion en cours Discussion fermée Problème résolu
Répondre
Titre du message :
Votre pseudo:
Votre email :
Message: 
  •  
  •  
Options: Recevoir les réponses par mail.
 

Aide