IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Entrée/Sortie Java Discussion :

génération d un fichier binaire a partir d un tableau


Sujet :

Entrée/Sortie Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    80
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 80
    Points : 54
    Points
    54
    Par défaut génération d un fichier binaire a partir d un tableau
    bonjour tt le monde voilà mon probleme consiste en ceci : je veux insérer les données d un tableau qui resulte de la concaténation de 4 tableaux dans un fichier exple :"septembre.bin"

    je dois aparemment créer un flux binaire de sortie (FileOutputStream) mais je ne sais vraiment pas comment faire et je ne sais pas si c'est la methode la plus adaptée a ce que moi j envisage de faire...si quelqu'un peut m aider merci

    voici mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
     
    import java.io.*;
     
     
     
     
    public class  GenererFMI_224_1{
     
    public static void main(String[] args){}
     
     
     
    public static byte [] getHeader()
    	{
    	byte [] tab= new byte [25];
     
    	tab[0]=	(byte)0x03;
     
    	tab[1]=	(byte)0x01;
     
    	tab[2]=	(byte)0x00;
     
    	tab[3]=	(byte)0x00;
    	tab[4]=	(byte)0x00;
    	tab[5]=	(byte)0x19;
     
    	tab[6]=	(byte)0x00;
    	tab[7]=	(byte)0x00;
    	tab[8]=	(byte)0x32;
     
    	tab[9]=	(byte)0x02;
     
    	tab[10]=(byte)0x00;
     
    	tab[11]=(byte)0x00;
    	tab[12]=(byte)0x00;
    	tab[13]=(byte)0x4B;
     
    	tab[14]=(byte)0x00;
    	tab[15]=(byte)0x09;
    	tab[16]=(byte)0x96;
     
    	tab[17]=(byte)0xE0;
    	tab[18]=(byte)0x00;
     
    	tab[19]=(byte)0x00;
    	tab[20]=(byte)0x09;	
    	tab[21]=(byte)0xE1;
     
    	tab[22]=(byte)0x00;
    	tab[23]=(byte)0x00;
    	tab[24]=(byte)0x5A;
     
     
    	return tab;
     
     
    }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    public static byte [] getFragmentManagement()
    	{
    	byte [] tab= new byte[50];
     
    	tab[0]=	(byte)0xFF;
    	tab[1]=	(byte)0x21;
    	tab[2]=	(byte)0x00;
    	tab[3]=	(byte)0x00;
    	tab[4]=	(byte)0x00;
    	tab[5]=	(byte)0x00;
    	tab[6]=	(byte)0x00;
    	tab[7]=	(byte)0x00;
    	tab[8]=	(byte)0x00;
    	tab[9]=	(byte)0x02;
     
    	tab[10]=(byte)0x00;
    	tab[11]=(byte)0x00;
    	tab[12]=(byte)0x00;
    	tab[13]=(byte)0x16;
    	tab[14]=(byte)0x00;
    	tab[15]=(byte)0x00;
    	tab[16]=(byte)0x00;
    	tab[17]=(byte)0x03;
    	tab[18]=(byte)0x00;
    	tab[19]=(byte)0x00;
    	tab[20]=(byte)0x00;
     
    	tab[21]=(byte)0x2C;
    	tab[22]=(byte)0x00;
    	tab[23]=(byte)0x00;
    	tab[24]=(byte)0x00;
    	tab[25]=(byte)0x04;
    	tab[26]=(byte)0x00;
    	tab[27]=(byte)0x00;
    	tab[28]=(byte)0x00;
    	tab[29]=(byte)0x42;
     
    	tab[30]=(byte)0x00;
    	tab[31]=(byte)0x00;
    	tab[32]=(byte)0x00;
    	tab[33]=(byte)0x05;
    	tab[34]=(byte)0x00;
    	tab[35]=(byte)0x00;
    	tab[36]=(byte)0x00;
    	tab[37]=(byte)0x4A;
    	tab[38]=(byte)0x00;
    	tab[39]=(byte)0x00;
     
    	tab[40]=(byte)0x00;
    	tab[41]=(byte)0x06;
    	tab[42]=(byte)0x00;
    	tab[43]=(byte)0x00;
    	tab[44]=(byte)0x00;
    	tab[45]=(byte)0x52;
    	tab[46]=(byte)0x00;
    	tab[47]=(byte)0x00;
    	tab[48]=(byte)0x00;
    	tab[49]=(byte)0x00;
    	tab[50]=(byte)0x07;
     
     
     
    	return tab;
     
     
     
    	}
     
     
    public static byte [] getDataRepository()
    		{	
    		try {	RandomAccessFile raf= new RandomAccessFile ("Result_221.1.dat", "r");
    			boolean fin=false;
    			int i=0;
    			raf.seek(50);
    			byte [] tab = new byte[2454];
    			int n=raf.read(tab);
    			System.out.println("buffer length "+n);
    				//bb[0]=b;
    				//System.out.print(new String (bb));
    				//i++;
     
    				//byte [] buffer=new byte[9407];
    			        //int n= raf8.read(buffer);
    			        //System.out.println("GZIP Buffer of length "+n);
    				//byte tab[]= new byte [9407 -964];
    			        //System.arraycopy(buffer,964, tab,0,(9407-964));
    			        //System.arraycopy(bb,0, buffer,51,(10000-35));	
    			return tab;	}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    		return null;
    	}
     
     
     
    public static byte[] getESGDataRepository()
    	{
    	byte[] tab= new byte[90];
     
    //00 00 13 00 23 10 f0 39 0 0 c3 c0 84 04 34 34 1c 1c 68 5c 87 83 00 84 13 00 23 10 f0 39 00 01 43 c0 84 04 34 34 1c 1c 5
     
    	tab[0]=(byte)0x00;
    	tab[1]=(byte)0x00;
    	tab[2]=(byte)0x13;
    	tab[3]=(byte)0x00;
    	tab[4]=(byte)0x23;
    	tab[5]=(byte)0x10;
    	tab[6]=(byte)0xF0;
    	tab[7]=(byte)0x39;
    	tab[8]=(byte)0x00;
    	tab[9]=(byte)0x00;
     
    	tab[10]=(byte)0xC3;
    	tab[11]=(byte)0xC0;
    	tab[12]=(byte)0x84;
    	tab[13]=(byte)0x04;
    	tab[14]=(byte)0x34;
    	tab[15]=(byte)0x34;
    	tab[16]=(byte)0x1C;
    	tab[17]=(byte)0x1C;
    	tab[18]=(byte)0x68;
    	tab[19]=(byte)0x5C;
    	tab[20]=(byte)0x87;
     
    	tab[21]=(byte)0x83;
    	tab[22]=(byte)0x00;
    	tab[23]=(byte)0x84;
    	tab[24]=(byte)0x13;
    	tab[25]=(byte)0x00;
    	tab[26]=(byte)0x23;
    	tab[27]=(byte)0x10;
    	tab[28]=(byte)0xF0;
    	tab[29]=(byte)0x39;
     
    	tab[30]=(byte)0x00;
    	tab[31]=(byte)0x01;
    	tab[32]=(byte)0x43;
    	tab[33]=(byte)0xC0;
    	tab[34]=(byte)0x84;
    	tab[35]=(byte)0x04;
    	tab[36]=(byte)0x34;
    	tab[37]=(byte)0x34;
    	tab[38]=(byte)0x1C;
    	tab[39]=(byte)0x1C;
     
    //c 87 83 01 08 13 00 23 10 f0 39 00 00 43 c0 84 04 34 34 1c 1c 68 5c 87 83 01 8a 05 00 25 10 02 1f 04 8e 05 00 25 10 
     
    //02 1f 07 12 05 00 25 10 02 1f
     
    	tab[40]=(byte)0x05;
    	tab[41]=(byte)0x0C;
    	tab[42]=(byte)0x87;
    	tab[43]=(byte)0x83;
    	tab[44]=(byte)0x01;
    	tab[45]=(byte)0x08;
    	tab[46]=(byte)0x13;
    	tab[47]=(byte)0x00;
    	tab[48]=(byte)0x23;
    	tab[49]=(byte)0x10;
    	tab[50]=(byte)0xF0;
    	tab[51]=(byte)0x39;
     
    	tab[52]=(byte)0x00;
    	tab[53]=(byte)0x00;
    	tab[54]=(byte)0x43;
    	tab[55]=(byte)0xC0;
    	tab[56]=(byte)0x84;
    	tab[57]=(byte)0x04;
    	tab[58]=(byte)0x34;
    	tab[59]=(byte)0x34;
    	tab[60]=(byte)0x1C;
     
    	tab[61]=(byte)0x1C;
    	tab[62]=(byte)0x68;
    	tab[63]=(byte)0x5C;
    	tab[64]=(byte)0x87;
    	tab[65]=(byte)0x83;
    	tab[66]=(byte)0x01;
    	tab[67]=(byte)0x8A;
    	tab[68]=(byte)0x05;
    	tab[69]=(byte)0x00;
    	tab[70]=(byte)0x25;
    	tab[71]=(byte)0x10;
     
    	tab[72]=(byte)0x02;
    	tab[73]=(byte)0x1F;
    	tab[74]=(byte)0x04;
    	tab[75]=(byte)0x8E;
    	tab[76]=(byte)0x05;
    	tab[77]=(byte)0x00;
    	tab[78]=(byte)0x25;
    	tab[79]=(byte)0x10;
    	tab[80]=(byte)0x02;
     
    	tab[81]=(byte)0x1F;
    	tab[82]=(byte)0x07;
    	tab[83]=(byte)0x12;
    	tab[84]=(byte)0x05;
    	tab[85]=(byte)0x00;
    	tab[86]=(byte)0x25;
    	tab[87]=(byte)0x10;
    	tab[88]=(byte)0x02;
    	tab[89]=(byte)0x1F;
     
     
     
    	return tab;
     
    }
    		/*FileOutputStream fos = null;
    		try
    		{
    			fos = new FileOutputStream("ESGAccessDescriptor.bin");
    			fos.write(bos.toByteArray());
    		}*/
     
     
      public byte[] concat(byte[] tab1, byte[] tab2, byte[] tab3){
            byte[] tabResult = new byte[tab1.length + tab2.length + tab3.length];
            int count = 0;
            for(int i=0; i<tab1.length; i++)
                tabResult[count++] = tab1[i];
            for(int i=0; i<tab2.length; i++)
                tabResult[count++] = tab2[i];
    	for(int i=0; i<tab3.length; i++)
                tabResult[count++] = tab3[i];
            return tabResult;
        }
     
    	byte[] result = concat(getHeader(), getFragmentManagement(), getDataRepository());
     
    };
    merci

  2. #2
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Points : 7 163
    Points
    7 163
    Par défaut
    Qu'est-ce que tu ne comprends pas dans la javadoc du FileOutputStream ?

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    80
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 80
    Points : 54
    Points
    54
    Par défaut
    Citation Envoyé par dinobogan Voir le message
    Qu'est-ce que tu ne comprends pas dans la javadoc du FileOutputStream ?
    merci pr ta reponse,
    en fait j ai modifié mon code comme suit, mais j ai l impression que jne m en sors pas...

    en fait j arrive a générer un flux binaire a condition d'entrer les octets un par un manuellement et apres j arrive a en generer un fichier binaire avec FileOutputStream..
    mais mon probleme c'est que j arrive pas a comprendre comment faire pour générer le flux binaire a partir de mon tableau "tableauFINAL"..j ai essayé comme ca mais aparemment j ai tout faux...merci de m aider..
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     public byte[] concat(byte[] tab1, byte[] tab2, byte[] tab3){
            byte[] tabResult = new byte[tab1.length + tab2.length + tab3.length];
            int count = 0;
            for(int i=0; i<tab1.length; i++)
                tabResult[count++] = tab1[i];
            for(int i=0; i<tab2.length; i++)
                tabResult[count++] = tab2[i];
    	for(int i=0; i<tab3.length; i++)
                tabResult[count++] = tab3[i];
            return tabResult;
        }
     
    	byte[] result = concat(getHeader(), getFragmentManagement(), getDataRepository());
     
    File fd = new File("septembre.bin");
     
    OutputStream fos = new FileOutputStream(fd);
     
    byte[] tableauFINAL = concat(getHeader(), getFragmentManagement(), getDataRepository());/
    fos.write(tableauFINAL);

  4. #4
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Points : 7 163
    Points
    7 163
    Par défaut
    Je ne comprends pas : ça marche ou ça marche pas ?

    Ta méthode n'est pas la plus efficace/optimisée, mais ça devrait fonctionner.
    Par exemple, inutile de concaténer les 3 tableaux dans un seul :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    fos.write( getHeader() );
    fos.write( getFragmentManagement() );
    fos.write( getDataRepository() );
    Et pense à fermer les flux !

    Ensuite, à l'avenir, lorsque tu auras une copie de tableau à faire, je t'encourage vivement à regarder "java.lang.System" dans la javadoc, particulièrement "arraycopy".

  5. #5
    Inactif  
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    497
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 497
    Points : 312
    Points
    312
    Par défaut
    'lut,


    Pourquoi ne pas sérialiser tout simplement c'est fait pour ...

    Définition :

    La sérialisation consiste à écrire des données présentes en mémoire vers un flux de données binaires.


    La sérialisation binaire en Java

Discussions similaires

  1. Génération d'un fichier xml a partir d'un model
    Par sovo dans le forum Langage
    Réponses: 2
    Dernier message: 03/09/2010, 18h29
  2. [MySQL] Gènèration d'un fichier XML a partir d'une formulaire PHP
    Par bitbot dans le forum PHP & Base de données
    Réponses: 12
    Dernier message: 28/07/2010, 17h49
  3. Réponses: 4
    Dernier message: 23/02/2007, 15h06
  4. créer un fichier vidéo a partir des images binaires
    Par jack_1981 dans le forum Langage
    Réponses: 4
    Dernier message: 17/11/2006, 19h35
  5. génération d'un fichier texte a partir d'une BDD
    Par devoracle dans le forum Forms
    Réponses: 7
    Dernier message: 02/11/2006, 11h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo