Ajouter un commentaire

Gefrey

C'est vraiment incroyable comme l'informaticien est irritable... A chaque fois que je pose une question (sur different forum) je me fais engueuler pour une raison ou pour une autre...

Bref, j'ai reussi a trouve ce que je cherchais. Maintenant j'aimerais recuperer les informations de mon fichier CSV. Dans quel variable je les trouve? Il me semble que c'est avec le vector mais je ne sias pas comment je peux l'utiliser. Je te donne le code pour lire le fichier CSV:

package flexcom.calltax.service.impl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Vector;

import flexcom.calltax.core.ExceptionHandler;

public class CSVFile {

private int m_rowsCount;
private int m_colsCount;
private Vector m_fileContent;
private final static char CELL_SEPARATOR = '+';


/**
* Method CSVFile.
* @param path le chemin du fichier à parser.
* @throws FileNotFoundException si le fichier spécifié n'existe pas.
*/
public CSVFile(String path) throws FileNotFoundException {

m_fileContent = new Vector();
System.out.println(path);
System.out.println(m_fileContent);
try{
FileReader fileReader = new FileReader(path);

readFromFile(fileReader);
fitVectorsToSize();


}catch(FileNotFoundException e){
ExceptionHandler.notify(e, CSVFile.class);
}
}

/**
* Method CSVFile.
* @param reader un reader dans lequel on lit le fichier CSV.
*/
public CSVFile(Reader reader) {

m_fileContent = new Vector();
readFromFile(reader);
fitVectorsToSize();
}

private void fitVectorsToSize() {
m_fileContent.setSize(getRowsCount());
int fileSize = getRowsCount();
int colCount = getColsCount();
for (int i = 0; i < fileSize; i++) {
Vector aRow = (Vector)m_fileContent.get(i);
if (aRow == null) {
m_fileContent.set(i, new Vector());
aRow = (Vector)m_fileContent.get(i);
}
aRow.setSize(colCount);
}
}

/**
* Method readFromFile.
* @param path
*/
private void readFromFile(Reader reader) {
BufferedReader buffReader = new BufferedReader(reader);
if (buffReader != null) {
try {
String tempLine;
tempLine = buffReader.readLine();
while (tempLine != null) {
readFromLine(tempLine);
tempLine = buffReader.readLine();
}
} catch (IOException e) {
System.err.println("Error reading CSV file: " + e.toString());
} finally {
try {
buffReader.close();
} catch (IOException e) {
System.err.println(
"Erreur closing CSV file: "
+ e.toString()
);
}
}
}
System.runFinalization();
System.gc();
}

/**
* Method readFromLine.
* @param tempLine
*/
private void readFromLine(String tempLine) {
if (tempLine == null) {
return;
}
Vector currentLine = new Vector();
m_fileContent.add(currentLine);
m_rowsCount++;
setRowsCount(getRowsCount() + 1);
if (tempLine.trim().length() == 0) {
return;
}
int colCount = 0;
int cursorBegin = 0;
int cursorEnd = tempLine.indexOf(CELL_SEPARATOR);
while (cursorBegin > -1) {
if (cursorEnd == -1) {
currentLine.add(tempLine.substring(cursorBegin));
cursorBegin = cursorEnd;
} else {
currentLine.add(tempLine.substring(cursorBegin, cursorEnd));
cursorBegin = cursorEnd + 1;
}
cursorEnd = tempLine.indexOf(CELL_SEPARATOR, cursorBegin);
colCount++;
}
if (colCount > getColsCount()) {
setColsCount(Math.max(getColsCount(), colCount));
}
}

/**
* Returns the colsCount.
* @return int
*/
public int getColsCount() {
return m_colsCount;
}

/**
* Returns the rowsCount.
* @return int
*/
public int getRowsCount() {
//m_rowsCount
return m_rowsCount;
}

/**
* Sets the colsCount.
* @param colsCount The colsCount to set
*/
public void setColsCount(int colsCount) {
m_colsCount = colsCount;
fitVectorsToSize();
}

/**
* Sets the rowsCount.
* @param rowsCount The rowsCount to set
*/
public void setRowsCount(int rowsCount) {
m_rowsCount = rowsCount;
fitVectorsToSize();
}

/**
* Method getData.
* @param row la ligne voulue
* @param col la colonne voulue
* @return String la valeur à l'enplacement spécifié. Null si outOfBound.
*/
public String getData(int row, int col) {
if (row < 0
|| col < 0
|| row > (getRowsCount() - 1)
|| col > (getColsCount() - 1)) {
return null;
}
try {
Vector theRow = (Vector)m_fileContent.get(row);
String result = (String)theRow.get(col);
return (result == null ? "" : result);
} catch (IndexOutOfBoundsException e) {
return "";
}
}
}

MERCI.

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
  SSS    QQQ     AA   Y   Y  U   U 
S Q Q A A Y Y U U
SSS Q Q AAAA Y U U
S Q QQ A A Y U U
SSSS QQQQ A A Y UUU
Q