파일 인코딩에 문제가 있습니다. 내가 만든 형식으로 내 DB를 XML로 내보내는 메소드가 있습니다. 문제는이 파일이 ANSI 인코딩으로 생성되었고 UTF-8 인코딩 (일부 스페인어 문자는 ANSI에서 제대로 표시되지 않음)이 필요하다는 것입니다.Java에서 stringbuilder 객체로 utf8 파일을 만드는 방법
XML 파일은 에서 생성됩니다. StringBuilder 개체 : 내 DB에서이 StringBuilder 개체에 데이터를 쓰고 모든 데이터를 복사 할 때 파일을 만들었습니다.
도움을 주신 모든 분들께 감사드립니다. advace에 감사드립니다.
편집 : XMLBuilder 클래스 :
XmlBuilder xml = new XmlBuilder();
... (adding to xml)...
xmlString = xml.build();
file = createXml(xmlString);
...
createXml : 나는 XML 파일을 생성
...
public XmlBuilder() throws IOException {
this.sb = new StringBuilder();
}
...
public String xmlBuild() throws IOException{
this.sb.append(CLOSE_DB);
return this.sb.toString();
}
...
서비스 클래스
이 내 소스의 일부입니다public File createXml(String textToFile) {
File folder = new File("xml/exported/");
if (!folder.exists()) {
folder.mkdirs();
}
file = new File("xml/exported/exportedData.xml");
try (FileOutputStream fop = new FileOutputStream(file)) {
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
//if file exists, then delete it and create it
else {
file.delete();
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = textToFile.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
파일을 작성한 코드를 표시 할 수 있습니까? 일반적으로 추가 매개 변수로 인코딩을 제공 할 수 있습니다. – Keppil
답장을 보내 주셔서 감사합니다 @Keppil. 나는 방금 내 질문을 소스 코드로 편집했다. – Alberto
아래 @ 키스의 대답을 시도, 나는 그것이 작동한다고 생각합니다. – Keppil