Résultats de 1 à 2 sur 2

Sujet : Ecriture dans un fichier

  1. #1

    Ecriture dans un fichier

    Bonjour zeb, viola j\'ai fini ma partie entrée des données dans un fichier et chez moi ca ne marche pas, pourrait tu regarder si mon code est good?

    import java.io.FileWriter;
    import java.io.IOException;

    public class Donne
    {
    String mInvest;
    String mRSupp;
    String tDac;
    String fMaint;
    String dVie;
    String rebus;


    // constructeur qui initialise les données a 0

    Donne()
    {
    mInvest = \"0\";
    mRSupp = \"0\";
    tDac = \"0\";
    fMaint = \"0\";
    dVie = \"0\";
    rebus = \"0\";
    }

    // constructeur qui accepte 6 valeurs en parametre pour initialiser les données

    Donne(String i,String rs,String a,String m,String v,String r)
    {
    try
    {
    FileWriter F;
    F = new FileWriter(\"doc.txt\", true);

    mInvest = i;
    for (int w = 0; w < mInvest.length();w++)
    {
    F.write(mInvest.charAt(w));
    }
    F.write(\"\\n\"

    mRSupp = rs;
    for (int w = 0; w < mRSupp.length();w++)
    {
    F.write(mRSupp.charAt(w));
    }
    F.write(\"\\n\"

    tDac = a;
    for (int w = 0; w < tDac.length();w++)
    {
    F.write(tDac.charAt(w));
    }
    F.write(\"\\n\"

    fMaint = m;
    for (int w = 0; w < fMaint.length();w++)
    {
    F.write(fMaint.charAt(w));
    }
    F.write(\"\\n\"

    dVie = v;
    for (int w = 0; w < dVie.length();w++)
    {
    F.write(dVie.charAt(w));
    }
    F.write(\"\\n\"

    rebus = r;
    for (int w = 0; w < rebus.length();w++)
    {
    F.write(rebus.charAt(w));
    }
    F.write(\"\\n\"
    F.close();
    }
    catch (IOException e)
    {
    System.out.println(\"Exception : \" + e.getMessage());
    }
    }

    void saisie()
    {
    try //pour tout ce qui est succeptible de faire une erreur
    {
    Mediator m = new Mediator();
    FileWriter F;
    F = new FileWriter(\"doc.txt\", true);

    // le true provoque un ajout en fin de fichier
    // sans lui tu ecrase ton fichier a chaque tour

    //on rentre le montant des investissements

    System.out.println(\"Quel est le montant de l\'investissement?\"
    mInvest = m.readString();

    for (int i = 0; i < mInvest.length();i++)
    {
    F.write(mInvest.charAt(i));
    }
    F.write(\"\\n\"

    //on rentre le montant des recettes supplementaires attendues grace a cet investissment

    System.out.println(\"Quel est le montant des recettes supplementaires attendues grace a cet investissment?\"
    mRSupp = m.readString();

    for (int i = 0; i < mRSupp.length();i++)
    {
    F.write(mRSupp.charAt(i));
    }
    F.write(\"\\n\"

    //on rentre le taux d\'actualisation

    System.out.println(\"Quel est le taux d\'actualisation?\"
    tDac = m.readString();

    for (int i = 0; i < tDac.length();i++)
    {
    F.write(tDac.charAt(i));
    }
    F.write(\"\\n\"

    //on rentre le montant eventuel des frais de maintenance entraines par cet investissement

    System.out.println(\"Quel est le montant eventuel des frais de maintenance entraines par cet investissement?\"
    fMaint = m.readString();

    for (int i = 0; i < fMaint.length();i++)
    {
    F.write(fMaint.charAt(i));
    }
    F.write(\"\\n\"

    //on rentre la duree de vie de cet innvestissement

    System.out.println(\"Quel est la duree de vie de cet innvestissement?\"
    dVie = m.readString();

    for (int i = 0; i < dVie.length();i++)
    {
    F.write(dVie.charAt(i));
    }
    F.write(\"\\n\"

    //on rentre la valeur residuelle de cet investissement lors de sa mise au rebut

    System.out.println(\"Quel est la valeur residuelle de cet investissement lors de sa mise au rebut?\"
    rebus = m.readString();

    for (int i = 0; i < rebus.length();i++)
    {
    F.write(rebus.charAt(i));
    }
    F.write(\"\\n\"

    F.close();

    }

    catch (IOException e) //pour l\'erreur trouvé
    {
    System.out.println(\"Exception : \" + e.getMessage());//on affiche ce message
    }
    }
    }


    Je te remercie, bonne journé
    ++

  2. #2
    Aussi je pense que pour compiler tu devrais avoir besoinde cette classe kon utilise pour les TP(je suis en 1ere année de DUT informatique)!!
    /**
    * This class enables performing operations: <ul>
    * <li> on standard input stream \"System.in\" ususally attached to keyboard,
    * <li> and on standard output stream \"System.out\" usually attached to screen </ul>
    *
    * @author Christophe Lecoutre
    * @author e-mail : lecoutre@cril.univ-artois.fr
    * @version 2.1
    */

    import java.io.*;

    public class Mediator
    {
    private final static ByteValueReader byteR = new ByteValueReader();
    private final static ShortValueReader shortR = new ShortValueReader();
    private final static IntValueReader intR = new IntValueReader();
    private final static LongValueReader longR = new LongValueReader();
    private final static FloatValueReader floatR = new FloatValueReader();
    private final static DoubleValueReader doubleR = new DoubleValueReader();
    private final static CharValueReader charR = new CharValueReader();
    private final static StringValueReader stringR = new StringValueReader();

    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static String line = new String();
    private static int position;

    /**
    * Read and return one input byte.
    * The returned value is in the range -2^7 through 2^7-1, inclusive.
    *
    * @return the 8-bit value read
    */
    public static byte readByte()
    {
    return byteR.get();
    }

    /**
    * Call readByte() after displaying a message.
    *
    * @param s the message displayed before reading.
    * @return the value returned by readByte().
    * @see Mediator#readByte()
    */
    public static byte readByte(String s)
    {
    System.out.print(s);
    return (byte) readInt();
    }

    /**
    * Read two input bytes and returns a short value.
    * The returned value is in the range -2^15 through 2^15-1, inclusive.
    *
    * @return the 16-bit value read
    */
    public static short readShort()
    {
    return shortR.get();
    }

    /**
    * Call readShort() after displaying a message.
    *
    * @param s the message displayed before reading.
    * @return the value returned by readShort().
    * @see Mediator#readShort()
    */
    public static short readShort(String s)
    {
    System.out.print(s);
    return (short) readInt();
    }

    /**
    * Read four input bytes and returns a int value.
    * The returned value is in the range -2^31 through 2^31-1, inclusive.
    *
    * @return the 32-bit value read
    */
    public static int readInt()
    {
    return intR.get();
    }

    /**
    * Call readInt() after displaying a message.
    *
    * @param s the message displayed before reading.
    * @return the value returned by readInt().
    * @see Mediator#readInt()
    */
    public static int readInt(String s)
    {
    System.out.print(s);
    return readInt();
    }

    /**
    * Call readInt() after displaying a message.
    *
    * @param s the message displayed before reading.
    * @return the value returned by readInt().
    * @see Mediator#readInt()
    * @exception IllegalArgumentException an exception is thrown if the value returned by readInt() is not included between inf and sup
    */
    public static int readInt(String s, int inf, int sup) throws IllegalArgumentException
    {
    System.out.print(s);
    int i = readInt();
    if (i < inf || i > sup)
    throw new IllegalArgumentException(\"from readInt\"
    return i;
    }

    /**
    * Read four input bytes and returns a int value.
    * The returned value is in the range -2^63 through 2^63-1, inclusive.
    *
    * @return the 64-bit value read
    */
    public static long readLong()
    {
    return longR.get();
    }

    /**
    * Call readLong() after displaying a message.
    *
    * @param s the message displayed before reading.
    * @return the value returned by readLong().
    * @see Mediator#readLong()
    */
    public static long readLong(String s)
    {
    System.out.print(s);
    return readLong();
    }

    /**
    * Read four input bytes and returns a float value.
    * The returned value is a representation of a floating-point value
    * according to the IEEE 754 floating-point \"single precision\" bit layout.
    *
    * @return the float value read.
    */
    public static float readFloat()
    {
    return floatR.get();
    }

    /**
    * Call readFloat() after displaying a message.
    *
    * @param s the message displayed before reading.
    * @return the value returned by readFloat().
    * @see Mediator#readFloat()
    */
    public static float readFloat(String s)
    {
    System.out.print(s);
    return readFloat();
    }

    /**
    * Read eight input bytes and returns a double value.
    * The returned value is a representation of a floating-point value
    * according to the IEEE 754 floating-point \"double precision\" bit layout.
    *
    * @return the double value read.
    */
    public static double readDouble()
    {
    return doubleR.get();
    }

    /**
    * Call readDouble() after displaying a message.
    *
    * @param s the message displayed before reading.
    * @return the value returned by readDouble().
    * @see Mediator#readDouble()
    */
    public static double readDouble(String s)
    {
    System.out.print(s);
    return readDouble();
    }

    /**
    * Read a char.
    *
    * @return the Unicode char read
    */
    public static char readChar()
    {
    return charR.get();
    }

    /**
    * Call readChar() after displaying a message.
    *
    * @param s the message displayed before reading.
    * @return the value returned by readChar().
    * @see Mediator#readChar()
    */
    public static char readChar(String s)
    {
    System.out.print(s);
    return readChar();
    }

    /**
    * Reads a line of text. A line is considered to be terminated by any one
    * of a line feed (\'\\n\'), a carriage return (\'\\r\'),
    * or a carriage return followed immediately by a linefeed.
    *
    * @return a String containing the contents of the line,
    * @return not including any line-termination characters.
    */
    public static String readString()
    {
    return stringR.get();
    }

    /**
    * Calls readString() after displaying a message.
    *
    * @param s the message displayed before reading.
    * @return the value returned by readString().
    * @see Mediator#readString()
    */
    public static String readString(String s)
    {
    System.out.print(s);
    return readString();
    }

    /**
    * Print a String.
    *
    * @param s The String to be printed.
    */
    public static void write(String s)
    {
    System.out.print(s);
    }

    /**
    * Print a String and then terminate the line.
    * @param s The String to be printed.
    */
    public static void writeln(String s)
    {
    System.out.println(s);
    }

    /**
    * Terminate the line.
    */
    public static void writeln()
    {
    System.out.println();
    }

    /**
    * Empty the buffer.
    */
    public static void flush()
    {
    position = line.length(); //ValueReader.flush();
    }

    private static abstract class ValueReader
    {
    void readNewLine()
    {
    try
    {
    line = br.readLine();
    position = 0;
    }
    catch (Exception e)
    {
    System.out.println(\"Exceptionnal error\"
    }
    }
    }

    private static abstract class NumericValueReader extends ValueReader
    {
    private int firstWhitespace()
    {
    int i = position;
    while (i < line.length() && !Character.isWhitespace(line.charAt(i)))
    i++;
    return i;
    }

    private int firstSignificantCharacter()
    {
    int i = 0;
    boolean found = false;

    while (!found)
    {
    if (position >= line.length())
    readNewLine();
    i = position;
    while (i < line.length() && Character.isWhitespace(line.charAt(i)))
    i++;
    found = (i != line.length());
    }
    return i;
    }

    abstract void transform(String line) throws NumberFormatException;

    void set()

    {
    boolean pb = true;
    int left, right;

    while (pb == true)
    {
    pb = false;
    position = left = firstSignificantCharacter();
    position = right = firstWhitespace();
    try
    {
    transform(line.substring(left, right));
    }
    catch (NumberFormatException e)
    {
    pb = true;
    }
    if (pb == true)
    System.out.print(\"typing error, try again : \"
    }
    }
    }

    private static class ByteValueReader extends NumericValueReader
    {
    private byte i;

    void transform(String s) throws NumberFormatException
    {
    i = Byte.parseByte(s);
    }
    public byte get()
    {
    set();
    return i;
    }
    }

    private static class ShortValueReader extends NumericValueReader
    {
    private short i;

    void transform(String s) throws NumberFormatException
    {
    i = Short.parseShort(s);
    }
    public short get()
    {
    set();
    return i;
    }
    }

    private static class IntValueReader extends NumericValueReader
    {
    private int i;

    void transform(String s) throws NumberFormatException
    {
    i = Integer.parseInt(s);
    }
    public int get()
    {
    set();
    return i;
    }
    }

    private static class LongValueReader extends NumericValueReader
    {
    private long i;

    void transform(String s) throws NumberFormatException
    {
    i = Long.parseLong(s);
    }
    public long get()
    {
    set();
    return i;
    }
    }

    private static class FloatValueReader extends NumericValueReader
    {
    private float i;

    void transform(String s) throws NumberFormatException
    {
    i = Float.parseFloat(s);
    }
    public float get()
    {
    set();
    return i;
    }
    }

    private static class DoubleValueReader extends NumericValueReader
    {
    private double i;

    void transform(String s) throws NumberFormatException
    {
    i = Double.parseDouble(s);
    }
    public double get()
    {
    set();
    return i;
    }
    }

    private static class CharValueReader extends ValueReader
    {
    private char firstCharacter()
    {
    while (position >= line.length())
    readNewLine();
    return line.charAt(position++);
    }

    public char get()
    {
    return firstCharacter();
    }
    }

    private static class StringValueReader extends ValueReader
    {
    public String get()
    {
    readNewLine();
    return line;
    }
    }
    }
    voila, je te remercie d\'avance

Informations du sujet

Utilisateur(s) parcourant ce sujet

il y a actuellement 1 utilisateur(s) parcourant ce sujet. (0 membre(s) et 1 visiteur(s))

Règles des messages

  • Vous ne pouvez pas créer de sujets
  • Vous ne pouvez pas répondre aux sujets
  • Vous ne pouvez pas importer de fichiers joints
  • Vous ne pouvez pas éditer vos messages
  •  
  • Les BB codes sont Activés
  • Les Smileys sont Activés
  • Le BB code [IMG] est Activé
  • Le code [VIDEO] est Activé
  • Le code HTML est Désactivé