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.