使用BufferedReader读取word文档,出现乱码,如何解决?

Zh_java_2009 2012-01-04 10:19:48

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class Test
{
public static void main (String args[]){
try {
String readStr = "";
String read = "";
FileInputStream fr = new FileInputStream("C:/default 1 testorg20.doc");
InputStreamReader is = new InputStreamReader(fr,"utf-8");
BufferedReader br=new BufferedReader(is);
while ((read = br.readLine()) != null) {
readStr = readStr + read;
}
System.out.println("------------>>"+readStr);
System.out.println("------------>>"+readStr.length());
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}


java代码如上。

那个恶心的文档连接如下:
http://m614.mail.qq.com/cgi-bin/download?mailid=ZC2504-AYPrgwO0Q4GCLStkbf4if21&filename=default+1+testorg20.doc&sid=52V2rS2crW9sDQtE

不知道怎么发附件,我是发在QQ邮箱里的。
...全文
932 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Zh_java_2009 2012-01-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 abstruct 的回复:]
下面是我移动文件的一段代码,可以完成doc的拷贝,你可以看看

Java code

public static void moveFile(File moveFile, String dir, String fileName){
File outDir = new File(dir);
if(!outDir.exists()){
……
[/Quote]
你这里的我看了,应该不能解决乱码的问题,不过还是谢了哈。
刚才朋友在网上给我找了个第三方插件,试了下,能搞定这些乱码问题。
http://weijie.blog.51cto.com/340746/87694
安特矮油 2012-01-04
  • 打赏
  • 举报
回复
下面是我移动文件的一段代码,可以完成doc的拷贝,你可以看看

public static void moveFile(File moveFile, String dir, String fileName){
File outDir = new File(dir);
if(!outDir.exists()){
outDir.mkdirs();
}

// InputStreamReader reader = null;
// OutputStreamWriter writer = null;
DataInputStream in = null;
DataOutputStream out = null;
try {
in = new DataInputStream(new FileInputStream(moveFile));
out = new DataOutputStream(new FileOutputStream(new File(dir + fileName)));

int i;
while((i=in.read()) != -1){
out.write(i);
}
out.flush();
if(in != null){
in.close();
moveFile.delete();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(out != null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Zh_java_2009 2012-01-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 helios_fly 的回复:]
试试将word文档的编码格式改为utf-8怎么样?或者将inputstreamReader的编码格式改为与word的相同!
[/Quote]

word编码格式怎么查看哦
Zh_java_2009 2012-01-04
  • 打赏
  • 举报
回复
先自己顶下吧。
iGoodLoser 2012-01-04
  • 打赏
  • 举报
回复
试试将word文档的编码格式改为utf-8怎么样?或者将inputstreamReader的编码格式改为与word的相同!

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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