代码:
DataOutputStream out=
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\test\\test.html.txt")));
out.writeUTF(s3);
out.close();
s3 为一个xml结构的文档:
<xml><head>
<meta id="p001"/>
<script/>
</head>
<body>
<label id="a001" dict="ture">
用户
</label>
<label id="a002" dict="false">
密码
</label>
</body>
</xml>
输出结果:
<xml><head>
<meta id="p001"/>
<script/>
</head>
<body>
<label id="a001" dict="ture">
用户
</label>
<label id="a002" dict="false">
密码
</label>
</body>
</xml>
多出来2个不知道是什么的东东。
DataOutputStream out=
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\test\\test.html.txt")));
out.writeUTF(s3);
out.close();
s3 为一个xml结构的文档:
<xml><head>
<meta id="p001"/>
<script/>
</head>
<body>
<label id="a001" dict="ture">
用户
</label>
<label id="a002" dict="false">
密码
</label>
</body>
</xml>
输出结果:
<xml><head>
<meta id="p001"/>
<script/>
</head>
<body>
<label id="a001" dict="ture">
用户
</label>
<label id="a002" dict="false">
密码
</label>
</body>
</xml>
多出来2个不知道是什么的东东。
public final void writeUTF(String str)
throws IOException以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流。
首先,通过 writeShort 之类的方法将两个字节写入输出流,表示后跟的字节数。该值是实际写出的字节数,不是字符串的长度。根据此长度,使用字符的 UTF-8 修改版编码按顺序输出字符串的每个字符。如果没有抛出异常,则计数器 written 增加写入输出流的字节总数。该值至少是 2 加 str 的长度,最多是 2 加 str 的三倍长度。
指定者:
接口 DataOutput 中的 writeUTF
参数:
str - 要写入的字符串。
抛出:
IOException - 如果发生 I/O 错误。
代码如下:
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\test\\test.html.txt"),"UTF-8"));
out.write(s3);
out.close();
结帖给分~