Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Encodage UTF-8 : questions de débutant

6 réponses
Avatar
Gart
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bonjour,
j'ai une question concernant le codage de caractères.
Je manipule des chaines contenant des accents, et j'aimerais enregistré
dans un fichier au format utf-8.
J'obtiens les données d'une base de données, et j'écris dans un fichier
pseudo XML pour qu'une autre application puisse lire ces données.
Je procède comme ceci :

String xmlSource = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?><RESULTATSRECHERCHE>";

if(res != null) {
try {
while(res.next()){
id = res.getString("id");
titre = res.getString("nom");
xmlSource += "<GROUPE>";
xmlSource += "<ID>"+id+"</ID>";
xmlSource += "<TITRE>"+titre+"</TITRE>";
xmlSource += "<URL>"+urlStock+"/groupe"+id+"/themes.xml"+"</URL>";
xmlSource += "</GROUPE>";
}
} catch (SQLException e1) {
e1.printStackTrace();
throw new RemoteException(e1.getMessage());

}

}
xmlSource += "</RESULTATSRECHERCHE>";
try {
xmlSource = java.net.URLEncoder.encode(xmlSource,"UTF-8");
} catch (UnsupportedEncodingException e1) {
Trace.println("Error while coding xml in UTF-8 : not supported");
e1.printStackTrace();
return;
}
Fichier groupeXml = new Fichier(urlStock+"/groupes.xml");
groupeXml.ecrire(xmlSource);
groupeXml.fermer();

En fait, je suis sur de n'avoir pas compris comment je devais faire pour
écrire mon fichier en utf-8. Si quelqu'un pouvais m'expliquer comment je
dois faire, parce que l'encodage de caractère me passe un peu au-dessus
de la tête pour l'instant.
D'avance merci,
Gart.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/nodcRxl9VpkOBpgRAoH1AJ9A1bFudFaGupgrXHnDCrcW5s1bqgCfT/cU
9uQZgz86vANZN7j4whsxS8Q=
=mLxD
-----END PGP SIGNATURE-----

6 réponses

Avatar
9OnLine
Je suis d'accord avec toi mais c'est ce que j'ai fait pour l'instant. Mais
je trouve que ça gonfle conciderablement la memoire de la JVM. Et en plus
j'ai vu une javadoc dispo avec un classloader qui peut le faire. Mais il y
avait pas les sources. :(

Donc je me demandais si quelqu'un n'aurait pas la solution ????

Et si quelqu'un pouvait me confirmer que ça confle bien DANGEREUSEMENT la
memoire de la JVM ???

Merci d'avance.


"Gart" a écrit dans le message news:
3f9e868b$0$13291$
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bonjour,
j'ai une question concernant le codage de caractères.
Je manipule des chaines contenant des accents, et j'aimerais enregistré
dans un fichier au format utf-8.
J'obtiens les données d'une base de données, et j'écris dans un fichier
pseudo XML pour qu'une autre application puisse lire ces données.
Je procède comme ceci :

String xmlSource = "<?xml version="1.0"
encoding="UTF-8"?><RESULTATSRECHERCHE>";

if(res != null) {
try {
while(res.next()){
id = res.getString("id");
titre = res.getString("nom");
xmlSource += "<GROUPE>";
xmlSource += "<ID>"+id+"</ID>";
xmlSource += "<TITRE>"+titre+"</TITRE>";
xmlSource += "<URL>"+urlStock+"/groupe"+id+"/themes.xml"+"</URL>";
xmlSource += "</GROUPE>";
}
} catch (SQLException e1) {
e1.printStackTrace();
throw new RemoteException(e1.getMessage());

}

}
xmlSource += "</RESULTATSRECHERCHE>";
try {
xmlSource = java.net.URLEncoder.encode(xmlSource,"UTF-8");
} catch (UnsupportedEncodingException e1) {
Trace.println("Error while coding xml in UTF-8 : not supported");
e1.printStackTrace();
return;
}
Fichier groupeXml = new Fichier(urlStock+"/groupes.xml");
groupeXml.ecrire(xmlSource);
groupeXml.fermer();

En fait, je suis sur de n'avoir pas compris comment je devais faire pour
écrire mon fichier en utf-8. Si quelqu'un pouvais m'expliquer comment je
dois faire, parce que l'encodage de caractère me passe un peu au-dessus
de la tête pour l'instant.
D'avance merci,
Gart.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/nodcRxl9VpkOBpgRAoH1AJ9A1bFudFaGupgrXHnDCrcW5s1bqgCfT/cU
9uQZgz86vANZN7j4whsxS8Q > =mLxD
-----END PGP SIGNATURE-----



Avatar
9OnLine
Salut,

Je pense qu'il faut que tu encodes simplement le contenu de tag XML qui
doivent être encodé et non le fichier entier.
Car si tu encodes la totalité du fichier il y aura des caractères XML encodé
et le parser ne si retrouvera plus.

Voici ce que serai ma version... :
try {
// Par defaut l'encodage ce fait en UTF-8 depuis le jdk 1.4
String szId = java.net.URLEncoder.encode(id);
String szTitre = java.net.URLEncoder.encode(titre);
String szUrl java.net.URLEncoder.encode(urlStock+"/groupe"+id+"/themes.xml");
xmlSource += "<GROUPE>";
xmlSource += "<ID>"+szId+"</ID>";
xmlSource += "<TITRE>"+szTitre+"</TITRE>";
xmlSource += "<URL>"+szUrl+"</URL>";
xmlSource += "</GROUPE>";
} catch (UnsupportedEncodingException e1) {
Trace.println("Error while coding xml in UTF-8 : not supported");
e1.printStackTrace();
return;
}
// Le bloque suivant n'est plus util
/*
try {
xmlSource = java.net.URLEncoder.encode(xmlSource,"UTF-8");
} catch (UnsupportedEncodingException e1) {
Trace.println("Error while coding xml in UTF-8 : not supported");
e1.printStackTrace();
return;
}
*/

File groupeXml = new File(urlStock+"/groupes.xml");
groupeXml.write(xmlSource);
groupeXml.close();

@+

"Gart" a écrit dans le message news:
3f9e868b$0$13291$
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bonjour,
j'ai une question concernant le codage de caractères.
Je manipule des chaines contenant des accents, et j'aimerais enregistré
dans un fichier au format utf-8.
J'obtiens les données d'une base de données, et j'écris dans un fichier
pseudo XML pour qu'une autre application puisse lire ces données.
Je procède comme ceci :

String xmlSource = "<?xml version="1.0"
encoding="UTF-8"?><RESULTATSRECHERCHE>";

if(res != null) {
try {
while(res.next()){
id = res.getString("id");
titre = res.getString("nom");
xmlSource += "<GROUPE>";
xmlSource += "<ID>"+id+"</ID>";
xmlSource += "<TITRE>"+titre+"</TITRE>";
xmlSource += "<URL>"+urlStock+"/groupe"+id+"/themes.xml"+"</URL>";
xmlSource += "</GROUPE>";
}
} catch (SQLException e1) {
e1.printStackTrace();
throw new RemoteException(e1.getMessage());

}

}
xmlSource += "</RESULTATSRECHERCHE>";
try {
xmlSource = java.net.URLEncoder.encode(xmlSource,"UTF-8");
} catch (UnsupportedEncodingException e1) {
Trace.println("Error while coding xml in UTF-8 : not supported");
e1.printStackTrace();
return;
}
Fichier groupeXml = new Fichier(urlStock+"/groupes.xml");
groupeXml.ecrire(xmlSource);
groupeXml.fermer();

En fait, je suis sur de n'avoir pas compris comment je devais faire pour
écrire mon fichier en utf-8. Si quelqu'un pouvais m'expliquer comment je
dois faire, parce que l'encodage de caractère me passe un peu au-dessus
de la tête pour l'instant.
D'avance merci,
Gart.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/nodcRxl9VpkOBpgRAoH1AJ9A1bFudFaGupgrXHnDCrcW5s1bqgCfT/cU
9uQZgz86vANZN7j4whsxS8Q > =mLxD
-----END PGP SIGNATURE-----



Avatar
Gart
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Merci, c'était simple mais j'avais un peu trop la tête dans le guidon,
je n'y ait pas pensé.

Nouvelle question maintenant, si je voulais écrire le fichier en utf-8,
sans coder les chaines qu'il contient, comment puis-je faire ?
Gart.

9OnLine wrote:
| Salut,
|
| Je pense qu'il faut que tu encodes simplement le contenu de tag XML qui
| doivent être encodé et non le fichier entier.
| Car si tu encodes la totalité du fichier il y aura des caractères XML
encodé
| et le parser ne si retrouvera plus.
|
| Voici ce que serai ma version... :
| try {
| // Par defaut l'encodage ce fait en UTF-8 depuis le jdk 1.4
| String szId = java.net.URLEncoder.encode(id);
| String szTitre = java.net.URLEncoder.encode(titre);
| String szUrl | java.net.URLEncoder.encode(urlStock+"/groupe"+id+"/themes.xml");
| xmlSource += "<GROUPE>";
| xmlSource += "<ID>"+szId+"</ID>";
| xmlSource += "<TITRE>"+szTitre+"</TITRE>";
| xmlSource += "<URL>"+szUrl+"</URL>";
| xmlSource += "</GROUPE>";
| } catch (UnsupportedEncodingException e1) {
| Trace.println("Error while coding xml in UTF-8 : not supported");
| e1.printStackTrace();
| return;
| }
| // Le bloque suivant n'est plus util
| /*
| try {
| xmlSource = java.net.URLEncoder.encode(xmlSource,"UTF-8");
| } catch (UnsupportedEncodingException e1) {
| Trace.println("Error while coding xml in UTF-8 : not supported");
| e1.printStackTrace();
| return;
| }
| */
|
| File groupeXml = new File(urlStock+"/groupes.xml");
| groupeXml.write(xmlSource);
| groupeXml.close();
|
| @+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/nqP1Rxl9VpkOBpgRAljWAJ4rBBGyJjLv5IrwJbBSCteX/89uMgCfeDXt
4BfBgmLwJAdSdNZHmFPDa5U =SWtE
-----END PGP SIGNATURE-----
Avatar
M6C
Peux-tu donner + d'explication car ce que tu dis me parait incohérent #@!%!!


"Gart" a écrit dans le message news:
3f9ea323$0$240$
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Merci, c'était simple mais j'avais un peu trop la tête dans le guidon,
je n'y ait pas pensé.

Nouvelle question maintenant, si je voulais écrire le fichier en utf-8,
sans coder les chaines qu'il contient, comment puis-je faire ?
Gart.

9OnLine wrote:
| Salut,
|
| Je pense qu'il faut que tu encodes simplement le contenu de tag XML qui
| doivent être encodé et non le fichier entier.
| Car si tu encodes la totalité du fichier il y aura des caractères XML
encodé
| et le parser ne si retrouvera plus.
|
| Voici ce que serai ma version... :
| try {
| // Par defaut l'encodage ce fait en UTF-8 depuis le jdk 1.4
| String szId = java.net.URLEncoder.encode(id);
| String szTitre = java.net.URLEncoder.encode(titre);
| String szUrl > | java.net.URLEncoder.encode(urlStock+"/groupe"+id+"/themes.xml");
| xmlSource += "<GROUPE>";
| xmlSource += "<ID>"+szId+"</ID>";
| xmlSource += "<TITRE>"+szTitre+"</TITRE>";
| xmlSource += "<URL>"+szUrl+"</URL>";
| xmlSource += "</GROUPE>";
| } catch (UnsupportedEncodingException e1) {
| Trace.println("Error while coding xml in UTF-8 : not supported");
| e1.printStackTrace();
| return;
| }
| // Le bloque suivant n'est plus util
| /*
| try {
| xmlSource = java.net.URLEncoder.encode(xmlSource,"UTF-8");
| } catch (UnsupportedEncodingException e1) {
| Trace.println("Error while coding xml in UTF-8 : not supported");
| e1.printStackTrace();
| return;
| }
| */
|
| File groupeXml = new File(urlStock+"/groupes.xml");
| groupeXml.write(xmlSource);
| groupeXml.close();
|
| @+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/nqP1Rxl9VpkOBpgRAljWAJ4rBBGyJjLv5IrwJbBSCteX/89uMgCfeDXt
4BfBgmLwJAdSdNZHmFPDa5U > =SWtE
-----END PGP SIGNATURE-----



Avatar
Frederic Lachasse
"Gart" wrote in message
news:3f9e868b$0$13291$
Bonjour,
j'ai une question concernant le codage de caractères.
Je manipule des chaines contenant des accents, et j'aimerais enregistré
dans un fichier au format utf-8.


Pour convertir/encoder des caractères java (Unicode 16 bits) en bytes dans
un stream, on utilise un java.io.OutputStreamWriter:

// Création d'un output stream sur un fichier
OutputStream os = new FileOutputStream(...);

// Writer qui va écrire dans le FileOutptuStream en UTF-8:
Writer out = new OutputStreamWriter(os, "UTF-8");

L'opération inverse (lire des bytes avec conversion en caractères) se fait
avec un java.io.InputStreamReader

Voir JavaDoc pour plus de détail, en particulier pour connaître le nom des
charsets supportés.

--
Frédéric Lachasse -

Avatar
Gart
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bonjour,
merci pour c'est indications, je m'empresse d'aller regarder. Je m'y
perd souvent avec tout les File, FileWriter et autres InputStream.

Gart.

Frederic Lachasse wrote:

| "Gart" wrote in message
| news:3f9e868b$0$13291$
|
|>Bonjour,
|>j'ai une question concernant le codage de caractères.
|>Je manipule des chaines contenant des accents, et j'aimerais enregistré
|>dans un fichier au format utf-8.
|
|
| Pour convertir/encoder des caractères java (Unicode 16 bits) en bytes dans
| un stream, on utilise un java.io.OutputStreamWriter:
|
| // Création d'un output stream sur un fichier
| OutputStream os = new FileOutputStream(...);
|
| // Writer qui va écrire dans le FileOutptuStream en UTF-8:
| Writer out = new OutputStreamWriter(os, "UTF-8");
|
| L'opération inverse (lire des bytes avec conversion en caractères) se fait
| avec un java.io.InputStreamReader
|
| Voir JavaDoc pour plus de détail, en particulier pour connaître le nom des
| charsets supportés.
|

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/n3n6Rxl9VpkOBpgRAhqYAKCCThZxPIxZ4q9CrO4mtiKZ4B8xqACfcFaG
1d8QsVkPq3ZGdQmGsoBkXHE =/mLX
-----END PGP SIGNATURE-----