0

Im trying to read my binary file that i created for a country with at least 5 gold medals. When im trying to read the file and then displaying it. it shows: ?????††††††††††††††???†††††††††††††††† 27 27 38 92. It displays part of the information. This is my code at the moment:

import java.io.*;
import java.util.*;
class Pays
    implements Comparable<Pays>
{

    private String nom;
    private int gold;
    private int silver;
    private int bronze;
    private int sum;

    public Pays(String nom,int gold,int silver, int bronze,int sum)
    {

        this.nom = nom;
        this.gold=gold;
        this.silver=silver;
        this.bronze=bronze;
        this.sum=sum;


    }

    public int compareTo(Pays autre) {

      return nom.toUpperCase().trim().compareTo( autre.nom.toUpperCase().trim() );
    }

    public String getNom(){ return nom; }
    public int getGold(){ return gold; }
    public int getSilver(){ return silver; }
    public int getBronze(){ return bronze; }
    public int getSum(){return sum;}

    public boolean equals(Object obj)
    {
        if (this == obj)
             return true;
        else
            if ( ! (obj instanceof Pays))
                  return false;
            else
            {
                Pays autre = (Pays) obj;
                return nom.trim().equalsIgnoreCase(autre.nom.trim());
            }
    }

    public void ecrire(DataOutputStream aCreer)
       throws IOException
       {    
            aCreer.writeBytes(nom);
            aCreer.writeInt(gold);
            aCreer.writeInt(silver);
            aCreer.writeInt(bronze);
            aCreer.writeInt(sum);
       }

}

class numba3{
    static LinkedList<Pays> lireCreer(String nomFichier)
        throws IOException
    { LinkedList<Pays> liste = new LinkedList<Pays>(); 
      boolean existeFichier = true ; 

       FileReader fr = null; 


       try {
             fr = new FileReader (nomFichier) ;
       }

       catch ( java.io.FileNotFoundException erreur) {
            System.out.println("Probleme d'ouvrir le fichier " +
                 nomFichier);
            existeFichier = false ; 
       }

       if (existeFichier) {


          BufferedReader entree = new BufferedReader(fr);
          boolean finFichier = false ;


          while ( !finFichier ) {

           String uneLigne = entree.readLine();


           if (uneLigne == null)
                finFichier = true ;
           else {

                String nom = uneLigne.substring(0, 38);
                int   gold   = Integer.parseInt(uneLigne.substring(38,43).trim());
                int silver = Integer.parseInt(uneLigne.substring(43,48).trim());
                int bronze  = Integer.parseInt(uneLigne.substring(48).trim());
                int sum=gold+silver+bronze;


                liste.add(new Pays(nom,gold,silver,bronze,sum));
           }
          }
         entree.close();
        }

      return liste;
    }
static void afficher(LinkedList<Pays> liste,int numero){
    for(int i=0;i<numero;i++){

        System.out.printf("%30s %10d %10d %10d %10d\n",liste.get(i).getNom(),liste.get(i).getGold(),liste.get(i).getSilver(),liste.get(i).getBronze(),liste.get(i).getSum());
    }
}
public static void quickSort(LinkedList<Pays> liste, int low, int high) {

        if (liste.isEmpty() == true || liste.size()== 0)
            return;

        if (low >= high)
            return;

        int middle = low + (high - low) / 2;
        int pivot = liste.get(middle).getSum();


        int i = low, j = high;
        while (i <= j) {
            while (liste.get(i).getSum() > pivot) {
                i++;
            }

            while (liste.get(j).getSum() < pivot) {
                j--;
            }

            if (i <= j) {
                Pays temp=liste.get(i);
                liste.set(i,liste.get(j));
                liste.set(j,temp);
                i++;
                j--;
            }
        }


        if (low < j)
            quickSort(liste, low, j);

        if (high > i)
            quickSort(liste, i, high);
    }
static void afficherPays(LinkedList<Pays>liste,String nom){
        for(int i=0;i<liste.size();i++){
            if(liste.get(i).equals(new Pays(nom, 0, 0, 0,0))){
                    System.out.printf("%30s %10d %10d %10d %10d\n",liste.get(i).getNom(),liste.get(i).getGold(),liste.get(i).getSilver(),liste.get(i).getBronze(),liste.get(i).getSum());

            }               
        }

    }   
static void creerBinaire(LinkedList<Pays> liste, int gold,String nomBinaire)    
        throws IOException
    {

      DataOutputStream aCreer = new DataOutputStream
                            ( new FileOutputStream(nomBinaire));

      for (int i = 0; i < liste.size(); i++)
      {
         if ( liste.get(i).getGold() >= gold)
                  liste.get(i).ecrire(aCreer);
      }

      aCreer.close();
      System.out.println("\nOn vient de creer le fichier binaire : " + nomBinaire);
    }

static LinkedList<Pays> readBinaire(String nomALire)
            throws IOException{
      System.out.println("On lit le fichier binaire du nom " + nomALire);

      LinkedList<Pays> unVect = new LinkedList<Pays> ();

      DataInputStream aLire = new DataInputStream
                            ( new FileInputStream(nomALire));

      boolean finFichier = false ;

      String nom = "";
      int rang = 0;

      while ( ! finFichier  ) {
        try {
            for (int i = 0; i < 19; i++)
             nom += aLire.readChar();

        } catch ( EOFException e ) {
            finFichier = true;
          }

        if (!finFichier) {
        int  gold=aLire.readInt(),
             silver=aLire.readInt(),
             bronze=aLire.readInt(),
             sum=aLire.readInt();

         Pays pers = new Pays(nom, gold, silver, bronze, sum);
         unVect.add(pers);

        }
      }
       aLire.close();
       return unVect;
    }


    public static void main(String[]args)throws IOException
        {
        LinkedList<Pays> liste = lireCreer("Olym_ete.txt");

        System.out.println("");
        System.out.println("1)Determine et afficher les 5 premier pays qui ont gagne plus de medailles en totale: ");
        quickSort(liste,0, liste.size()-1);
        afficher(liste,5);

        System.out.println("");
        System.out.println("2)Afficher les information des pays FRANCE, JAPON, ESPAGNE: ");
        afficherPays(liste,"FRANCE");
        afficherPays(liste,"JAPON");
        afficherPays(liste,"ESPAGNE");

        System.out.println("");
        System.out.println("3)Cree a partir de la liste un fichier binaire qui ont gagne au moins de 5 medailles en or: ");
        creerBinaire(liste, 5,"AtLeastFiveGold.bin");
        LinkedList<Pays> plusKeFive = readBinaire("AtLeastFiveGold.bin");
        System.out.println(plusKeFive.size());
        System.out.printf("%30s %10d %10d %10d %10d\n",plusKeFive.get(1).getNom(),plusKeFive.get(1).getGold(),plusKeFive.get(1).getSilver(),plusKeFive.get(1).getBronze(),plusKeFive.get(1).getSum());
    }
}

I can't figure out how to get rid of the symbols. I suspect that i have made a mistake while reading the binary file in readBinaire() but i cannot seem to figure out where i went wrong.

2
  • 1
    What do you expect to appear when you display a binary file? Commented Dec 2, 2014 at 20:11
  • im trying to read the binary file and converting it Commented Dec 2, 2014 at 20:23

1 Answer 1

3

A char in Java is a 16-bit Unicode character. A "byte" is an 8-bit byte.

You're using writeBytes to write a string as 38 bytes, but readChar to read them back in as 19 characters. writeBytes will write the low order 8 bits of each 16-bit character of the string. So if your string was "ABCD", the bytes it would write are

0x41 0x42 0x43 0x44

Then when you read them back in as 16-bit characters, it will read the first two characters either as

0x4142 0x4344

or

0x4241 0x4443

depending on how the byte ordering is handled. Either way, instead of "ABCD", you will get some strange Unicode characters whose code points are U+4142 or U+4241, and U+4344 or U+4443. (They are probably not displaying correctly.)

Sign up to request clarification or add additional context in comments.

5 Comments

As an addition: OP should use aCreer.writeChars(nom);.
@Tom Yes, but other adjustments have to be made. nom appears to be a 38-character string. So he will either need to change his loop to read in 38 characters instead of 19. Or if he intends something different (like maybe he really wants to write 8-bit bytes to save file space), he'll need to do something different on input. I'm not sure what he really wants, which is why I left it open what he ought to do.
you are correct but when i put 38 it gives me the list size of 9 which is wrong. Since there are 16 countries with at least 5 gold medals and not 9. So when i put in 19 it gives me 16 as the size.
@A.T Did you change writeBytes(nom) to writeChars(nom)? Or did you only change 19 to 38? If you do just the second one, of course the list size will be all wrong because you're reading more characters than you write for each country.
yes and now the symbols are gone but the the information are all over the place when i display it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.