xml写问题

jzhaohua 2006-08-30 09:38:40
如下代码:
Document 型变量 doc

FileOutputStream outStream = new FileOutputStream(文件); 
OutputStreamWriter outWriter = new OutputStreamWriter(outStream); 
((XmlDocument) doc).write(outWriter, "GB2312"); 
outWriter.close(); 
outStream.close();  

写的文件每行之间有空行,即使刚读出文件再写回去也会多出空行来,
请教如何解决?
...全文
190 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jzhaohua 2006-09-01
  • 打赏
  • 举报
回复
各位好心人,用我贴的代码测试一下吧,会不会发生这样的情况?
sundeveloper 2006-08-31
  • 打赏
  • 举报
回复
读出来放在byte[]中,,
再转成String,去其空格...
husl 2006-08-31
  • 打赏
  • 举报
回复
没遇到过 帮顶一下
fengfangfang 2006-08-30
  • 打赏
  • 举报
回复
读写使用相同的编码方式
jzhaohua 2006-08-30
  • 打赏
  • 举报
回复
以下是代码:
public class t {
private DocumentBuilderFactory dbf = null; // 程序使用的变量
private DocumentBuilder db = null; // 程序使用的变量
private XmlDocument doc = null; // 程序使用的变量
private Element root; // 程序使用的变量

public void a(String file) throws Exception
{
dbf = DocumentBuilderFactory.newInstance();
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
System.err.println(pce);
}

try {
doc = (XmlDocument)db.parse(new File(file));
} catch (DOMException dom) {
System.err.println(dom.getMessage());
} catch (IOException ioe) {
System.err.println(ioe);
} catch (SAXException sax) {
System.err.println(sax.getMessage());
} catch (Exception e) {
System.err.println(e.getMessage());
}

root = doc.getDocumentElement();

System.out.println(this.root);
FileOutputStream outStream = new FileOutputStream(file);
OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
doc.write(outWriter, "GB2312");
outWriter.close();
outStream.close();

}
}
执行后打开文件,发现每两行间多了一行,内容是一个或两个 tab 符,数量和深度相同。
有遇到过的吗?

读的时候怎么指定编码方式?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧