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 :

Java : Ecriture BOM ds fichier


Sujet :

Entrée/Sortie Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 6
    Par défaut Java : Ecriture BOM ds fichier
    Bonjour,

    Je travaille sur un projet de génération de diagramme dia à partir d'un fichier Excel. Afin d'être correctement interprété par Dia, le fichier généré doit commencer par les octets FFFE (caracètre BOM - Byte Ordre Mark). Je ne parviens pas à écrire ce caractère en début de fichier. Pouvez-vous m'aider ?

    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
     
    //copie de l'en-tête type de dia, avec le BOM
    	FileInputStream in=new FileInputStream("Header.dia");
    	FileOutputStream out=new FileOutputStream(strChemin);
    	FileChannel src=in.getChannel();
    	FileChannel dest=out.getChannel();
    	src.transferTo(0, src.size(), dest);
    	src.close();
    	dest.close();
    //a ce niveau, le fichier généré contient bien le BOM
     
    	bw=new BufferedWriter(new FileWriter(new File(strCheminFichier), true));
    	strTemp="\n";
    	bw.write(strTemp);
    	bw.flush();
    	for (int i=0; i<nbNoeud; i++)
    	{
    	(...)
    	//tous les arguments ci-dessous sont de type String
    	bw.write(strBackground+currentNoeud.getConstat()+strEndString+strNewLine);
    	bw.flush();
    	//a la fin de la premiere boucle, le fichier contient toujours le BOM
    	//après la deuxième boucle, le fichier ne contient plus le BOM !
    	}
    	bw.close();
    Voyez-vous d'où vient le problème et comment le résoudre ?

  2. #2
    Membre expérimenté Avatar de jiddou
    Inscrit en
    Août 2007
    Messages
    247
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 247
    Par défaut
    Le problème est dans la boucle alors essaie de nous donner son contenu
    Je veux voir le contenu de
    strEndString
    strNewLine
    strBackground
    Le maximum d'informations s'il te plait

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 6
    Par défaut
    Voici la boucle complète :

    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
     
    for (int i=0; i<nbNoeud; i++)
    //parcours de tous les noeuds
    	{
    	numNoeud=i+1;
    	currentNoeud=this.eltAt(i);
    	x=currentNoeud.getX();
    	y=currentNoeud.getY();
    	//ajout d'un objet Box
    	strTemp=strObjectBoxHead+i+"\">"+strNewLine;
    	bw.write(strObjectBoxHead+i+"\">"+strNewLine);
    	bw.flush();
    	//ajout des coordonnées
    	strTemp=strObjectPos+x+".0,"+y+".0\""+strEndLine;
    	bw.write(strObjectPos+x+".0,"+y+".0\""+strEndLine);
    	bw.flush();
    	//ajout des caractéristiques 'corner'
    	strTemp=strObjectEltCorner+x+".0,"+y+".0\""+strEndLine;
    	bw.write(strObjectEltCorner+x+".0,"+y+".0\""+strEndLine);
    	bw.flush();
    	//TODO : calculer dynamiquement le EW
    	strTemp=strObjectEltWidth+"5"+"\""+strEndLine;
    	bw.write(strObjectEltWidth+"5"+"\""+strEndLine);
    	bw.flush();
    	//TODO : calculer dynamiquement le EH
    	strTemp=strObjectEltHeight+"1.9000000000000004"+"\""+strEndLine;
    	bw.write(strObjectEltHeight+"1.9000000000000004"+"\""+strEndLine);
    	bw.flush();
    	//ajout du texte
    	strTemp=strBackground+currentNoeud.getConstat()+strEndString+strNewLine;
    	bw.write(strBackground+currentNoeud.getConstat()+strEndString+strNewLine);
    	bw.flush();
    	//fermeture de l'objet
    	strTemp=strEndObject;
    	bw.write(strEndObject);
    	bw.flush();
    }

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 6
    Par défaut
    Pardon, j'ai oublié la déclaration des variables ... Ces déclarations sont faites avant la boucle. Voici le contenu des variables :

    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
     
    	String strNewLine="\n";		//saut de ligne
    	//chaînes préformatées au format dia
    	String strObjectBoxHead="    <dia:object type=\"Flowchart - Box\" version=\"0\" id=\"O";
    	String strEndLine="/>"+strNewLine;
    	String strRealVal="        <dia:real val=\"";
    	String strBoolVal="        <dia:boolean val=\"";
    	String strPointVal="        <dia:point val=\"";
    	String strEndString="#</dia:string>";
    	String strEndAttribute="      </dia:attribute>"+strNewLine;
    	String strAttributeName="      <dia:attribute name=\"";
     
    	//chaînes préformatées au format dia incluant plusieurs lignes
    	String strObjectPos=strAttributeName+"obj_pos\">"+strNewLine;
    	strObjectPos+=strPointVal;
     
    	String strObjectEltCorner=strEndAttribute;
    	strObjectEltCorner+=strAttributeName+"obj_bb\">"+strNewLine;
    	strObjectEltCorner+="        <dia:rectangle val=\"0.0,0.0;0.0,0.0\"/>"+strNewLine;
    	strObjectEltCorner+=strEndAttribute;
    	strObjectEltCorner+=strAttributeName+"elem_corner\">"+strNewLine;
    	strObjectEltCorner+=strPointVal;
     
    	String strObjectEltWidth=strEndAttribute;
    	strObjectEltWidth+=strAttributeName+"elem_width\">"+strNewLine;
    	strObjectEltWidth+=strRealVal;
     
    	String strObjectEltHeight=strEndAttribute;
    	strObjectEltHeight+=strAttributeName+"elem_height\">"+strNewLine;
    	strObjectEltHeight+=strRealVal;
     
    	String strBackground=strEndAttribute;
    	strBackground+=strAttributeName+"show_background\">"+strNewLine;
    	strBackground+=strBoolVal+"true\"/>"+strNewLine;
    	strBackground+=strEndAttribute;
    	strBackground+=strAttributeName+"padding\">"+strNewLine;
    	strBackground+=strRealVal+"0.5\"/>"+strNewLine;
    	strBackground+=strEndAttribute;
    	strBackground+=strAttributeName+"text\">"+strNewLine;
    	strBackground+="        <dia:composite type=\"text\">"+strNewLine;
    	strBackground+=strAttributeName+"string\">"+strNewLine;
            strBackground+="            <dia:string>#";
    L'objet currentNoeud est de type Noeud, une classe que j'ai définit. Les types renvoyés par les méthodes appelés ci-dessus :
    getX() et getY() renvoient des entiers (int)
    getConstat() renvoient une chaîne de caractères (String)

  5. #5
    Membre expérimenté Avatar de jiddou
    Inscrit en
    Août 2007
    Messages
    247
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 247
    Par défaut
    J'ai pas vu réellement ou est le problème mais je suis sûre que tu peux le regler avec juste un peu d'algorithme.Voilà donc comme algo je te propose d'améliorer ton code en écrivant dans le fichier après ta boucle.Donc ta boucle ne sert qu'à préparer la chaîne à écrire avec plusieurs concaténation puis l'écriture se passe à la fin.

    Voici à quoi va ressembler ton 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
    String strChaineFinale="";
    
    String strNewLine="\n"; //saut de ligne
    //chaînes préformatées au format dia
    String strObjectBoxHead=" <dia:object type=\"Flowchart - Box\" version=\"0\" id=\"O";
    String strEndLine="/>"+strNewLine;
    String strRealVal=" <dia:real val=\"";
    String strBoolVal=" <dia:boolean val=\"";
    String strPointVal=" <dia:point val=\"";
    String strEndString="#</dia:string>";
    String strEndAttribute=" </dia:attribute>"+strNewLine;
    String strAttributeName=" <dia:attribute name=\"";
    
    //chaînes préformatées au format dia incluant plusieurs lignes
    String strObjectPos=strAttributeName+"obj_pos\">"+strNewLine;
    strObjectPos+=strPointVal;
    
    String strObjectEltCorner=strEndAttribute;
    strObjectEltCorner+=strAttributeName+"obj_bb\">"+strNewLine;
    strObjectEltCorner+=" <dia:rectangle val=\"0.0,0.0;0.0,0.0\"/>"+strNewLine;
    strObjectEltCorner+=strEndAttribute;
    strObjectEltCorner+=strAttributeName+"elem_corner\">"+strNewLine;
    strObjectEltCorner+=strPointVal;
    
    String strObjectEltWidth=strEndAttribute;
    strObjectEltWidth+=strAttributeName+"elem_width\">"+strNewLine;
    strObjectEltWidth+=strRealVal;
    
    String strObjectEltHeight=strEndAttribute;
    strObjectEltHeight+=strAttributeName+"elem_height\">"+strNewLine;
    strObjectEltHeight+=strRealVal;
    
    String strBackground=strEndAttribute;
    strBackground+=strAttributeName+"show_background\">"+strNewLine;
    strBackground+=strBoolVal+"true\"/>"+strNewLine;
    strBackground+=strEndAttribute;
    strBackground+=strAttributeName+"padding\">"+strNewLine;
    strBackground+=strRealVal+"0.5\"/>"+strNewLine;
    strBackground+=strEndAttribute;
    strBackground+=strAttributeName+"text\">"+strNewLine;
    strBackground+=" <dia:composite type=\"text\">"+strNewLine;
    strBackground+=strAttributeName+"string\">"+strNewLine;
    strBackground+=" <dia:string>#";
    
    
    for (int i=0; i<nbNoeud; i++)
    //parcours de tous les noeuds
    {
    numNoeud=i+1;
    currentNoeud=this.eltAt(i);
    x=currentNoeud.getX();
    y=currentNoeud.getY();
    //ajout d'un objet Box
    strTemp=strObjectBoxHead+i+"\">"+strNewLine;
    strChaineFinale+=strTemp;
    //ajout des coordonnées
    strTemp=strObjectPos+x+".0,"+y+".0\""+strEndLine;
    strTemp=strObjectBoxHead+i+"\">"+strNewLine;
    strChaineFinale+=strTemp;
    //ajout des caractéristiques 'corner'
    strTemp=strObjectEltCorner+x+".0,"+y+".0\""+strEndLine;
    strChaineFinale+=strTemp;
    //TODO : calculer dynamiquement le EW
    strTemp=strObjectEltWidth+"5"+"\""+strEndLine;
    strChaineFinale+=strTemp;
    //TODO : calculer dynamiquement le EH
    strTemp=strObjectEltHeight+"1.9000000000000004"+"\""+strEndLine;
    strChaineFinale+=strTemp;
    //ajout du texte
    strTemp=strBackground+currentNoeud.getConstat()+strEndString+strNewLine;
    strChaineFinale+=strTemp;
    } 
    
    bw.write(strTemp);
    bw.flush();
    bw.close();

  6. #6
    Membre expérimenté Avatar de jiddou
    Inscrit en
    Août 2007
    Messages
    247
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 247
    Par défaut
    En fait il y'a quelque qui est louche dans ton code. A quoi sert strTemp si tu écrases toujours son contenu sans l'utiliser avant.

  7. #7
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 6
    Par défaut
    C'est juste un oubli de ma part : initialement, j'utilisais la chaîne strTemp dans mes appels à bw.write. Compte tenu du problème que je rencontrais,j'ai modifié l'appel pour vérifier si ca ne venait pas de l'initialisation de strTemp. J'ai oublié de remettre l'appel initial ensuite

    J'ai modifié mon code selon tes préconisations, mais cela ne change rien Vois-tu une solution pour que je puisse inscrire cet octet en début de fichier ?
    Et pour ton optimisation, est-ce que cela ne risque pas d'être trop gourmand en mémoire ?

Discussions similaires

  1. Réponses: 3
    Dernier message: 10/04/2007, 16h09
  2. Probléme d'ecriture dans un fichier texte en java
    Par oldscrout dans le forum Entrée/Sortie
    Réponses: 6
    Dernier message: 19/01/2007, 19h10
  3. Ecriture dans un fichier avec une applet Java
    Par foued_scorpion dans le forum Applets
    Réponses: 1
    Dernier message: 25/10/2006, 11h30
  4. APPLET JAVA - Ecriture de fichiers sur le serveur ?
    Par cstan dans le forum Applets
    Réponses: 10
    Dernier message: 15/09/2006, 13h52
  5. [DOM] Ecriture d'un fichier XML en java
    Par fidififouille dans le forum Format d'échange (XML, JSON...)
    Réponses: 11
    Dernier message: 12/05/2004, 09h32

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