Lire le contenu d'un fichier sans connaitre leur nom

cs_dammak Messages postés 61 Date d'inscription samedi 16 octobre 2004 Statut Membre Dernière intervention 25 novembre 2007 - 31 déc. 2005 à 10:54
cs_dammak Messages postés 61 Date d'inscription samedi 16 octobre 2004 Statut Membre Dernière intervention 25 novembre 2007 - 1 janv. 2006 à 15:58
salut pour tout le monde ,
voila j'ai un probleme sérieux, je veux un code source d'un programme en java qui lire le contenu de dernier fichier copiée dans un répertoire bien spécifié autrement dit on a comme entré le chemin de fichier mais pas le nom j'ai une idée mais j'ai pas le réaliser c'est que a chaque fois un fichier se place dans ce répertoire en supprime tout ce qui existe dans le repertoire saufe le dernier pour le lire mais il reste le probleme de connaitre le nom de ce fichier. comme j'ai deja dit je veux le code source svp
mercie

diablo

2 réponses

gmi19oj19 Messages postés 545 Date d'inscription lundi 10 mai 2004 Statut Membre Dernière intervention 28 septembre 2011 2
31 déc. 2005 à 12:03
Ben j'comprends pas ce que tu veux là... tonFichier.getName() ne te convient pas ??

Puisque qu'en gros, si j'a bien saisi, tu recuperes tous les fichiers de ton repertoire (listFiles()), tu chopes leur date de dernière modif (lastModified()), tu les compares et tu prends la plus recente. A partir de là, tu recupères bien un objet de type File qui correspond à ton fichier. J'ai bon ?

Le code, ça donnerait ça :

File rep = new File(chemin du repertoire);
File[] liste = rep.listFiles();
int[] dateModif = new int[liste.length]
int index = max(dateModif);
String nomFichier = liste[index].getName();

ou max te renvoie la valeur maximale du tableau, ie la plus grand date donc la dernière.

gmi19oj19
0
cs_dammak Messages postés 61 Date d'inscription samedi 16 octobre 2004 Statut Membre Dernière intervention 25 novembre 2007
1 janv. 2006 à 15:58
bon merci pour votre aide c'est trés gentille
je vous donne les codes sources que j'utilise dans mon programme avec l'explication pour que tu comprend de quoi je parle
voila
la classe Lirecsv.java qui est un package beans
//package Beans.csv;
import java.io.Serializable ;
import java.util.*;
import java.io.*;
public class Lirecsv implements java.io.Serializable
{
private String path;
File id = new File("C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\Eurotherm\WEB-INF\classes\Beans\csv");

public void setPath(String s) {this.path = s;}
public String getPath() {return path;}




public Vector lecture()
{
Vector v = new Vector();

try
{
CSVFile cs = new CSVFile(path);
int col = cs.getColsCount();
int row = cs.getRowsCount();
for (int i=0; i<row; i++)
{

v.addElement(cs.getData(i,0)+","+cs.getData(i,1)+","+cs.getData(i,2)+","+(cs.getData(i,3))+","+cs.getData(i,4)+","+cs.getData(i,5)+","+cs.getData(i,6)+","+cs.getData(i,7)+","+cs.getData(i,8)+","+cs.getData(i,9)+","+cs.getData(i,10)+","+cs.getData(i,11)+","+(cs.getData(i,12))+","+(cs.getData(i,13)));
}

}
catch(Exception e)
{

}
return v;
}
}


la classe CSVFile qui va lire les fichier csv c'est aussi un package beans

package Beans.csv ;
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 = ';';

/**
* 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 (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();
}
}



ensuite voila la page jsp

qui a chaque chargement fait une liaison avec labase de données pour stocker le contenu des fichier

la question comment modifier ses programme de tel sort je donne le nom du répertoire ou le chemain au lieu du nom et sachan que je veux lire que le deernier fichier placé dans ce répertoire
0
Rejoignez-nous