Probleme fichier séquentiels en java

westerndigit Messages postés 161 Statut Membre -  
westerndigit Messages postés 161 Statut Membre -
Fichier séquentiels

Je souhaite créer un fichier de contact
avec RS(*); Nom, Prénom, Mail

1er avec une fonction existe renvoi vrai si la personne existe et faux si la personne n'existe pas
2eme une fonction ajouter RS, Nom, Prénom, Mail
3eme une fonction Trouve la personne d'après son RS entrée

(*) Raison Sociale.

Je voudrais savoir si mes codes sont corrects.

Merci,

_____________________________________________________________

/*
* Main.java
*
* Created on
*/

/**
*
* @author
*/
/*Classe Main

import java.io.*;
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException,ClassNotFoundException{
int choix;
GlobalPersonne c = new GlobalPersonne();
Fichier f=new Fichier();
if(f.ouvrir("lecture"))
{
c=f.lire();
f.fermer();
}

do
{
System.out.println("\n");
choix=menu();
switch(choix)
{
case 1 : c.ajouterPersonne();
break;
case 2 : c.rechercherPersonne();
break;
case 3 : c.afficherListe();
break;
case 4 : f.ouvrir("ecriture");
f.ecrire(c);
f.fermer();
System.out.println("fichier sauvegarder!");
break;
case 5 : System.exit(0);
break;

}
}while(choix!=5);
}
/**menu vers les differentes partie du programme*/
public static int menu()
{

int choix;
System.out.println("MENU PRINCIPAL");
System.out.println("--------------");
System.out.println("1.Ajouter une personne");
System.out.println("2.Rechercher une personne");
System.out.println("3.Afficher la liste");
System.out.println("4.Sauvegarder");
System.out.println("5.Sortir");
System.out.println("--------------");
System.out.print("votre choix : ");
choix=Lire.i();
return choix;
}

}

______________________________________________________________

/*
* Existe.java
*
* Created on
*/

/**
*
* @author
*/
/*Classe Existe

import java.io.*;

static int getNuméroClient(String nom, String prénom)
{
try {
if (!(new File("c:\\Fichier.txt")).exists()) return -1;
String strNumLu, NomLu, PrénomLu; MailLu; int NumLu;
FileReader fichierPersonne = new FileReader("c:\\Fichier.txt");
BufferedReader br = new BufferedReader(fichierPersonne);
while ((strNumLu = br.readLine()) != null)
{
NumLu = Integer.parseInt(strNumLu);
NomLu = br.readLine();
PrénomLu = br.readLine();
MailLu = br.readLine();
if (NomLu.compareTo(nom)==0 && PrénomLu.compareTo(prénom)==0) && MailLu.compareTo(mail)==0)
return NumLu;
}

fichierClient.close();
} catch (Exception e) { System.out.println(e.getMessage()); }
return -1;
}

_____________________________________________________________

/*
* AjouterClient.java
*
* Created on
*/

/**
*
* @author
*/
/*Classe AjouterClient

import java.io.*;

static int ajouteClient(String nom, String prénom)
{
if (getTrouve(nom, prénom, mail) != -1)
return getNuméroPersonne(nom, prénom, mail);
try {
int newNumPersonne = newNuméroPersonne();
FileWriter fichierPersonne = new FileWriter("c:\\Fichier.txt", true);
fichierPersonne.write(newNumClient + "\r\n");
fichierPersonne.write(nom + "\r\n");
fichierPersonne.write(prénom + "\r\n");
fichierPersonne.write(mail + "\r\n");
fichierPersonne.close();
return newNumPersonne;
} catch (Exception e) { System.out.println(e.getMessage()); }
return 0;
}
static void ajoutePersonne(int Rs, Nom, Prénom, Mail)
{
try {
int newPersonne = newPersonne();
FileWriter fichierPersonne = new FileWriter("c:\\Fichier.txt", true);
fichierPersonne.write(Rs + "\r\n");
fichierPersonne.write(Nom + "\r\n");
fichierPersonne.write(Prenom + "\r\n");
fichierPersonne.write(Mail + "\r\n");
fichierPersonne.close();
} catch (Exception e) { System.out.println(e.getMessage()); }
}

______________________________________________________________

/*
* Trouve.java
*
* Created on
*/

/**
*
* @author
*/
/*Classe Trouve

import java.io.*;

static int getTrouve(String rs,)
{
try {
if (!(new File("c:\\Fichier.txt")).exists()) return -1;
String strNumLu, NomLu, PrénomLu; MailLu int NumLu;
FileReader fichierClient = new FileReader("c:\\Fichier.txt");
BufferedReader br = new BufferedReader(fichierClient);
while ((strNumLu = br.readLine()) != null)
{
NumLu = Integer.parseInt(strNumLu);
NomLu = br.readLine();
PrénomLu = br.readLine();
MailLu=br.readLine();
if (NomLu.compareTo(nom)==0 && PrénomLu.compareTo(prénom)==0 && MailLu.compareTo(mail)==0);
return NumLu;
}
fichierClient.close();
} catch (Exception e) { System.out.println(e.getMessage()); }
return -1;
}

2 réponses

  1. KX Messages postés 19031 Statut Modérateur 3 020
     
    Si ça fonctionne comme tu veux c'est correct, sinon ça ne l'est pas...
    0
    1. westerndigit Messages postés 161 Statut Membre
       
      Bizarre comme réponse - ni oui ni non...
      réponse non résolue pour moi.
      0
    2. KX Messages postés 19031 Statut Modérateur 3 020
       
      La réponse est pourtant à la hauteur de la question...
      0
    3. westerndigit Messages postés 161 Statut Membre
       
      Pourquoi répondre si c'est pour marquer des conneries KX ? je mets -1
      0
  2. danimo
     
    Salut,

    C'est quoi GlobalPersonne ?

    Dan
    0
    1. westerndigit Messages postés 161 Statut Membre
       
      erreur de ma part c'est Trouve.java c'est l'embrouille total pour moi. Si ça continue je renonce à faire le code j'en ai marre...
      0