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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
package com.sinser.nms.util;
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;
/**
* @author Glob
* @version 0.1
*/
public class CSVFile {
private int m_rowsCount;
private int m_colsCount;
private Vector m_fileContent;
private final static char CELL_SEPARATOR = ',';
private final static char STRING_SEPARATOR = '\"';
private final static String CELL_STRING_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();
FileReader fileReader = new FileReader(path);
readFromFile(fileReader);
fitVectorsToSize();
}
/**
* 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 (cursorEnd == -1) {
currentLine.add(tempLine.substring(cursorBegin));
cursorBegin = cursorEnd;
cursorEnd = tempLine.indexOf(CELL_SEPARATOR, cursorBegin);
colCount++;
} else {
char startString = tempLine.charAt(cursorBegin);
if (startString == STRING_SEPARATOR) {
//cursorEnd = tempLine.indexOf(STRING_SEPARATOR, cursorBegin+1);
cursorEnd = tempLine.indexOf(CELL_STRING_SEPARATOR);
currentLine.add(tempLine.substring(cursorBegin+1, cursorEnd));
cursorBegin = cursorEnd + 2;
colCount++;
cursorEnd = tempLine.indexOf(CELL_SEPARATOR, cursorBegin);
} 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() {
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 "";
}
}
/**
* Method setData.
* @param row le numéro de ligne (commence à 0).
* @param col le numéro de colonne (commence à 0).
* @param data les données à insérer.
*/
public void setData(int row, int col, String data) {
if (row < 0
|| col < 0
|| row > (getRowsCount() - 1)
|| col > (getColsCount() - 1)) {
throw new IndexOutOfBoundsException();
}
Vector theRow = (Vector)m_fileContent.get(row);
theRow.setElementAt(data, col);
}
/**
* Method write.
* @param filePath le fichier dans lequel sauver les données.
* @throws IOException si une erreur survient.
*/
public void write(String filePath) throws IOException {
FileWriter fileWriter = new FileWriter(filePath);
write(fileWriter);
}
/**
* Method write.
* @param aWriter le writer dans lequel on veut écrire les données.
* @throws IOException si une erreur survient.
*/
public void write(Writer aWriter) throws IOException {
BufferedWriter writer;
writer = new BufferedWriter(aWriter);
int fileSize = getRowsCount();
int colCount = getColsCount();
for (int i = 0; i < fileSize; i++) {
for (int j = 0; j < colCount; j++) {
writer.write(getData(i, j));
if (j + 1 < colCount) {
writer.write(CELL_SEPARATOR);
}
}
if (i + 1 < fileSize) {
writer.write("\n");
}
}
writer.flush();
writer.close();
}
} |
Partager