Posez votre question Signaler

Lecture fichiers OO avec java

philches 2Messages postés 18 août 2008Date d'inscription - Dernière réponse le 20 août 2008 à 08:47
Bonjour,
Je débute en JAVA, qui pourra me dire si il est possible de lire/ecrire des fichiers open office (ods et odt) avec java ?
D'avance, merci.
Lire la suite 

Lecture fichiers OO avec java »

3 réponses
Réponse
+2
moins plus
Salut,

Fais (si tu veux) un essai avec ce code, je n'ai pas de fichiers .ods et .odt pour le faire.

import java.io.*;

class LireLignesFichier

{
static String fic = "";
// les lignes en // qui suivent me m'evitent, pour mes tests, d'entrer le nom du fichier
// Je procede par copie (Ctrl + c) de l'un des noms et, a la demande d'intro le colle (Ctrl + v)
// static String fic = "TabTextListesJ2sev142.txt";
// static String fic = "LireLignesFichier.java";
// static String fic = "LireLignesFichier.class";
// static String fic = "LireLignesFichier";
// static String fic = "LireLignesFichierX.java"; // fichier inexistant
// static String fic = "DateCalendar.java";
// static String fic = "FirstApplet.html";
static String extension = "";
static InputStream is = null;
static InputStreamReader isr = null;
static BufferedReader br = null;
static BufferedReader clavier = null;
static String ligne;
static String lignepartielle;
static int ctr = 0;
static int ctrlignes = 0;
static int lgline = 0;

public static void main(String[] argv) throws IOException

{
int ind = 0;
do
{
fic = "";
try
{
fic = (String)javax.swing.JOptionPane.showInputDialog
(null,
"Entrez le nom du fichier (avec son extension)\n" // message
+ "OU rien pour sortir",
"Saisir un nom de fichier", // titre
javax.swing.JOptionPane.PLAIN_MESSAGE, // type message
null, // icone
null,
"LireLignesFichier.java"); // Nom par defaut
}

catch (NullPointerException npe)

{
System.out.println(npe.toString());
javax.swing.JOptionPane.showMessageDialog(null, "Le texte entré est\n" + fic);
fic = "";
}
if (fic.equals(""))
return;
ind = fic.lastIndexOf(".");
if (ind == -1)
javax.swing.JOptionPane.showMessageDialog
(null,"Pas d'extension, Recommencer !");
}
while(ind == -1);
extension = fic.substring(ind);
String titre = "Liste d'un fichier" + extension;
do
{
try
{
is = new FileInputStream(fic);
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String ligne;
while ((ligne = br.readLine())!= null)
{ System.out.println("________________________________________"
+ "________________________________________");
javax.swing.JOptionPane.showMessageDialog(null,mess + "\n\n"
+ "... et fin.");
javax.swing.JOptionPane.showMessageDialog
(null,"Consulter éventuellement la console.");
br.close();
}
//catch (FileNotFoundException fnfe)

catch (IOException ioe)

{
System.out.println(ioe.toString());
javax.swing.JOptionPane.showMessageDialog(null," Fichier "
+ fic + " non trouve");
}


catch (RuntimeException re)

{
System.out.println(re.toString());
javax.swing.JOptionPane.showMessageDialog(null,"Voir la console");
}
fic = (String)javax.swing.JOptionPane.showInputDialog
(null, "Entrez un autre nom de fichier (avec son extension)\n"
+ "OU rien pour sortir");
}
while (!fic.equals(""));
return;
}
}

C'est un extrait, j'espere qu'il ne manque rien pour l'essai.

Tiens moi au courant...

Cordialement.


Dan
Ajouter un commentaire
Réponse
+1
moins plus
RE bonjour,

L'extrait ci-dessus n'est pas bon, c'est celui-ci qu'il faut utiliser :

import java.io.*;

class LireFichier

{
static String fic = "";
static String extension = "";
static InputStream is = null;
static InputStreamReader isr = null;
static BufferedReader br = null;
static BufferedReader clavier = null;
static String ligne;
static String lignepartielle;
static int ctr = 0;
static int ctrlignes = 0;
static int lgline = 0;


public static void main(String[] argv) throws IOException

{
int ind = 0;
do
{
fic = "";
try
{
fic = (String)javax.swing.JOptionPane.showInputDialog
(null,
"ENTRER le nom du fichier (AVEC SON EXTENSION)\n" // message
+ "OU rien pour sortir",
"SAISIR un nom de fichier", // titre
javax.swing.JOptionPane.PLAIN_MESSAGE, // type message
null, // icone
null,
"LireFichier.java"); // Nom par defaut
}

catch (NullPointerException npe)

{
System.out.println(npe.toString());
javax.swing.JOptionPane.showMessageDialog(null, "Le texte entre est\n" + fic);
fic = "";
}
if (fic.equals(""))
return;
ind = fic.lastIndexOf(".");
if (ind == -1)
javax.swing.JOptionPane.showMessageDialog
(null,"Pas d'extension, Recommencer !");
}
while(ind == -1);
extension = fic.substring(ind);
String titre = "Liste d'un fichier" + extension;
do
{
try
{
is = new FileInputStream(fic);
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String ligne;
while ((ligne = br.readLine())!= null)
{
System.out.println(ligne);
}
System.out.println("________________________________________"
+ "________________________________________");
javax.swing.JOptionPane.showMessageDialog
(null,"Consulter éventuellement la console.");
br.close();
}
catch (IOException ioe)

{
System.out.println(ioe.toString());
javax.swing.JOptionPane.showMessageDialog(null," Fichier "
+ fic + " non trouve");
}


catch (RuntimeException re)

{
System.out.println(re.toString());
javax.swing.JOptionPane.showMessageDialog(null,"Voir la console");
}
fic = (String)javax.swing.JOptionPane.showInputDialog
(null, "Entrez un autre nom de fichier (avec son extension)\n"
+ "OU rien pour sortir");
}
while (!fic.equals(""));
return;
}
}
philches - 20 août 2008 à 08:47
Bonjour,
Tout d'abord merci pour le code envoyé; Je vais m'en inspirer et je te tiens au courant, mais çà va pas être pour tout de suite, j'ai d'autres taches et travaux qui me sont arrivées hier.
En tout cas , merci encore.
A bientôt.
Ajouter un commentaire
Ce document intitulé « lecture fichiers OO avec java » issu de CommentCaMarche (www.commentcamarche.net) est mis à disposition sous les termes de la licence Creative Commons. Vous pouvez copier, modifier des copies de cette page, dans les conditions fixées par la licence, tant que cette note apparaît clairement.
Dossier à la une
Passage au tout numérique : quel coût pour les particuliers ?