Java + fichier.txt

inama1 Messages postés 10 Date d'inscription lundi 26 mars 2012 Statut Membre Dernière intervention 18 mai 2012 - 28 mars 2012 à 11:44
inama1 Messages postés 10 Date d'inscription lundi 26 mars 2012 Statut Membre Dernière intervention 18 mai 2012 - 28 mars 2012 à 17:09
Bonjour;
Qui peut m'aider à enregistrer les informations du output Netbeans dans un fichier.txt à partir d'un code java , puis lire ce fichier
Merciiii d'avance
Cordiallement INAMA

4 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
28 mars 2012 à 12:02
Bonjour,

Voici une classe qui me permet d'enregistrer et de lire des fichiers texte, elle devrait t'aider :



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 *
 * GestionFichiers est la classe qui permet de lire et ecrire des
 * fichiers texte a partir d'unse liste de chaines de caracteres
 *
 * @author Julien
 * @version 1.0
 */
public class GestionFichiers {

private final String FIN_LIGNE="\n";

/**
 * L'unique instance du singleton
 */
private static final GestionFichiers instance = new GestionFichiers();

/**
 * Methode d'acces à l'instance  de GestionFichiers
 * @return l'instance de GestionFichiers
 */
public static final GestionFichiers getInstance(){
return instance;
}

/**
 * Constructeur prive du singleton
 */
private GestionFichiers(){
super();
}

/**
 * fonction qui lit le contenu d'un fichier
 * @param nomFichier 
 * 	nom du fichier en entree
 * @return la liste des lignes
 */
public String lireFichier(String fichier){
StringBuilder sb = new StringBuilder();
try{
BufferedReader buff = new BufferedReader(new FileReader(fichier));
try {
String line;
while ((line = buff.readLine()) != null) {
sb.append(line).append(FIN_LIGNE);
}
} 
finally {
buff.close();
}
} 
catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}

/**
 * Cette procédure écrit dans le fichier les lignes de la liste
 * @param nomFichier 
 * 	nom du fichier dans lequel écrire
 * @param lignes 
 * 	liste des lignes à écrire
 */
public void ecrireFichier(String nomFichier, List<String> lignes) {
PrintWriter out=null;
try{
out = new PrintWriter(new BufferedWriter(new FileWriter(nomFichier)));
for(String s : lignes){
out.println(s);
}
}
catch(IOException exc){
exc.printStackTrace();
}
out.close();
}

public void append(String filename, String text) {
FileWriter writer = null;
try{
writer = new FileWriter(filename, true);
writer.write(text, 0, text.length());
}
catch(IOException ex){
ex.printStackTrace();
}
finally{
if(writer != null){
try {
writer.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
0
inama1 Messages postés 10 Date d'inscription lundi 26 mars 2012 Statut Membre Dernière intervention 18 mai 2012
28 mars 2012 à 15:34
c'est le code java qui m'a permis de connecter à un switch et d'exécuter les commandes :

import java.io.*;
>import java.io.PrintStream;
>import java.io.InputStream;
>import java.io.OutputStream;
>import java.net.Socket;
>public final class telnet
>{
> public telnet ()
> {}
>private static String command="show version";
>private TelnetClient telnet = new TelnetClient();
>private InputStream in;
>private PrintStream out;
>private FileOutputStream fop;
>private char prompt = '$';
>
>public telnet( String server, String user, String password ) {
>try {
>// Connect to the specified server
>telnet.connect( server, 23 );
>
>// Get input and output stream references
>in = telnet.getInputStream();
>out = new PrintStream( telnet.getOutputStream() );
>
>// Log the user on
>readUntil( "Username: " );
>write( user );
>readUntil( "Password: " );
>write( password );
>
>// Advance to a prompt
>readUntil("DCN_SW_TUN9000_SRV#" );
> write ( command );
>}
>catch( Exception e ) {
>}
>}
>
>public void su( String password ) {
>try {
>write( "su" );
>readUntil( "Password: " );
>write( password );
>prompt = '#';
>readUntil("DCN_SW_TUN9000_SRV# " );
>
>}
>catch( Exception e ) {
>}
>}
>
>public String readUntil( String pattern ) {
>try {
>char lastChar = pattern.charAt( pattern.length() - 1 );
>StringBuilder sb = new StringBuilder();
>boolean found = false;
>char ch = ( char )in.read();
>while( true ) {
>System.out.print( ch );
>sb.append( ch );
>if( ch == lastChar ) {
>if( sb.toString().endsWith( pattern ) ) {
>return sb.toString();
>}
>}
>ch = ( char )in.read();
>}
>}
>catch( Exception e ) {
>}
>return null;
>}
>
>public void write( String value ) {
>try {
>out.println( value );
>out.flush();
>System.out.println( value );
>}
>catch( Exception e ) {
>}
>}
>
>public String sendCommand( String command ) {
>try {
>write( command );
>return readUntil( prompt + " " );
>}
>catch( Exception e ) {
>}
>return null;
>}
>
>public void disconnect() {
>try {
>telnet.disconnect();
>}
>catch( Exception e ) {
>}
>}
>
>public static void main( String[] args ) {
>try {
>telnet telnet = new telnet( "172.22.152.60",
>"stg",
>"stg123" );
>telnet.sendCommand("");
>telnet.su( "root-password" );
>String sendCommand = telnet.sendCommand(command);
>
>telnet.disconnect();
>
>
>
>}
>catch( Exception e ) {
>}
>}}





Apres "runfile" j'obtient la res suivante :

>DCN_SW_TUN9000_SRV uptime is 1 year, 44 weeks, 5 days, 1 hour, 21 minutes
>System returned to ROM by power-on
>System restarted at 11:11:17 GMT+1 Thu May 20 2010
>System image file is "flash:c3560e-universal-mz.122-52.SE/c3560e-universal-mz.122-52.SE.bin"
>
>License Level: ipbase
>License Type: Permanent
>Next reload license Level: ipbase
>
>cisco WS-C3560E-24TD (PowerPC405) processor (revision G0) with 131072K bytes of memory.
>Processor board ID FDO1402R2TC
> --More--  Last reset from power-on
> --More--


je veux enregistrer cette resultat dans un fichier.txt avec un code java :)


Merciiiiiiiiii de m'aider :)))))
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
28 mars 2012 à 15:51
Tu as tout ce qu'il te faut dans le code que je t'ai donné. A toi de t'en servir.
0
inama1 Messages postés 10 Date d'inscription lundi 26 mars 2012 Statut Membre Dernière intervention 18 mai 2012
28 mars 2012 à 17:09
MERCI
0
Rejoignez-nous