Fichiers binaires en java

cilasasuki Messages postés 14 Date d'inscription   Statut Membre Dernière intervention   -  
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour, ma question est :
j'utilise le langage java, j'ai des tables HashMap à stocker dans des fichiers binaires et les recuperer plutard, j'ai essayer de le réaliser avec l'exemple suivant:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author FATARE
*/
public class Exemplewtable {
public static void main (String args[]) throws FileNotFoundException, IOException{
File fichier2= new File("fichier1");
FileOutputStream sortie = new FileOutputStream(fichier2);
DataOutputStream eregistrement1 ;

eregistrement1 = new DataOutputStream(new BufferedOutputStream(sortie));

FileInputStream entree = new FileInputStream(fichier2);
DataInputStream auteur1;
auteur1 = new DataInputStream(new BufferedInputStream(entree));

HashMap <String,Integer> table = new HashMap<String,Integer> ();

table.put("jght",44);
table.put("tftght",443);
table.put("htft",4435);
for (Map.Entry<String,Integer> entre: table.entrySet()){

eregistrement1.writeUTF(entre.getKey());

eregistrement1.writeInt(entre.getValue());




}eregistrement1.close();
try

{

System.out.println("auteur1 "+ auteur1.readUTF());

System.out.println( "auteur1 "+ auteur1.readInt());

auteur1.close();

}
catch (IOException t)
{
}

}}

l'exécution de ce pg me donne :

run:
auteur1 jght
auteur1 44
BUILD SUCCESSFUL (total time: 3 seconds).
mon but et de stocker ces table et pouvoir acceder à leurs contenu à partir d'un autre programme,par exemple dans un programme je teste si un string (abc) se trouve dans cette table si oui retourner sa valeur.merci d'avance.je voudrais au moins des indices .
A voir également:

2 réponses

skymax406 Messages postés 218 Date d'inscription   Statut Membre Dernière intervention   24
 
Pourquoi pas utiliser SQLite ?
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Ce que tu devrais faire c'est directement sérialiser ta HashMap.
Ça te donnera un vrai fichier binaire Java, facilement réutilisable par la suite.

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException
{
    File fichier = new File("fichier");
    
    // Création
    
    HashMap<String,Integer> map1 = new HashMap<String,Integer> ();
        map1.put("Tata",123);
        map1.put("Titi",456);
        map1.put("Toto",789);
    
    // Sérialisation
    
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fichier));
    out.writeObject(map1);
    out.close();
    
    // Désérialisation
    
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(fichier));    
    HashMap<String,Integer> map2 = (HashMap<String, Integer>) in.readObject();
    in.close();
    
    // Affichage
    
    for (Entry<String, Integer> e : map2.entrySet())
        System.out.printf("%s\t%d\n",e.getKey(),e.getValue());        
}
0