java - How to keep character "&" from ISO-8859-1 to UTF-8 -
i'd written java file using eclipse encoding iso-8859-1. in file, want create string such (in order create xml content , save database) :
// <image><img src="path_of_picture"></image> string xmlcontent = "<image><img src=\"" + path_of_picture+ "\"></image>";
in file, string , create new string constructor :
string mynewstring = new string(xmlcontent.getbytes(), "utf-8");
in order understood xml parser, xml content must converted :
<image><img src="path_of_picture"></image>
unfortunately, can't find how write xmlcontent result in mynewstring. tried 2 methods :
// first : string xmlcontent = "<image><img src=\"" + content + "\"></image>"; // result mynewstring = <image><img src="path_of_picture"></image> // , xml parser can't content of <image/> //second : string xmlcontent = "<image><img src=\"" + content + "\"></image>"; // result mynewstring = <image>&lt;img src="path_of_picture"&gt;</image>
do have idea ?
this unclear. strings don't have encoding. when write
string s = new string(someotherstring.getbytes(), someencoding);
you various results depending on default encoding setting (which used getbytes()
method).
if want read file encoded iso-8859-1, do:
- read bytes file:
byte[] bytes = files.readallbytes(path);
- create string using file's encoding:
string content = new string(bytes, "iso-8859-1);
if need write file utf-8 encoding do:
- convert string bytes utf-8 encoding:
byte[] utfbytes = content.getbytes("utf-8");
- write bytes file:
files.write(path, utfbytes);
Comments
Post a Comment