Forum de discussion
Forum « Programmation JAVA » (archives)
Re: fichier java
Envoyé: 18 août 2004, 7h16 par Hack_Track
<code>
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
/*
* Created on Aug 18, 2004
*
*/
/**
* @author Philippe Fery
*
*/
public class MatriceFile extends File {
public static final String MATRICE_SEPARATOR_CHAR = ",";
private BufferedWriter writer = null;
private BufferedReader reader = null;
public MatriceFile(String pathname) throws IOException {
super(pathname);
}
public void saveData(String[][] data) throws IOException {
writer = new BufferedWriter(new FileWriter(this));
int dataSize = data.length;
writer.write("" + dataSize);
writer.newLine();
for (int i = 0; i < dataSize; i++) {
String[] dataLine = data[i];
for (int j = 0; j < dataLine.length; j++) {
String dataValue = dataLine[j];
if (j != 0) {
writer.write(MATRICE_SEPARATOR_CHAR);
}
writer.write(dataValue);
}
writer.newLine();
}
writer.flush();
writer.close();
writer = null;
}
public String[][] readData() throws IOException {
int rowCount = getMatriceSize();
int colCount = getElementsCountPerLine();
String data[][] = new String[rowCount][colCount];
getReader();
String dataLine = "";
String value = "";
StringTokenizer tokenizer = null;
for (int row = 0; row < rowCount+1; row++) {
dataLine = reader.readLine();
if (row != 0) {
tokenizer = new StringTokenizer(dataLine,MATRICE_SEPARATOR_CHAR);
for (int col = 0; col < colCount; col++) {
value = (String) tokenizer.nextElement();
data[row-1][col] = value;
}
}
}
reader = null;
return data;
}
public int getMatriceSize() throws NumberFormatException, IOException {
getReader();
int matriceSize = Integer.parseInt(reader.readLine());
reader = null;
return matriceSize;
}
private int getElementsCountPerLine() throws IOException {
getReader();
reader.readLine();
String firstDataLine = reader.readLine();
reader = null;
StringTokenizer tokenizer = new StringTokenizer(firstDataLine, MATRICE_SEPARATOR_CHAR);
return tokenizer.countTokens();
}
private void getReader() throws FileNotFoundException {
reader = new BufferedReader(new FileReader(this));
}
public static void main(String args[]) {
try {
MatriceFile mf = new MatriceFile("D:/matriceData.txt");
String[][] newData = { { "a", "b", "c" }, {
"e", "f", "g" }, {
"i", "j", "k" }, {
"l", "m", "n" }
};
mf.saveData(newData);
System.out.println("Matrice size = " + mf.getMatriceSize());
System.out.println("Elements per line = " + mf.getElementsCountPerLine());
System.out.println("Data = ");
String[][] data = mf.readData();
for (int row = 0; row < data.length; row++) {
String[] dataLine = data[row];
for (int col = 0; col < dataLine.length; col++) {
String value = dataLine[col];
System.out.println(value+" ");
}
System.out.println("\n\r");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
</code>
;-)
HackTrack
Réponses
|