菜鸟询问一个关于datainputstream的问题

cjbkemp 2014-05-11 12:39:01
package com.test;

import java.io.IOException;
import java.util.Date;

public class Demo {

public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
Demo1 d1=new Demo1();
d1.demo1();
Demo2 d2=new Demo2();
d2.demo1();


}

}
package com.test;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

class Demo1 {
void demo1() throws IOException{
FileInputStream fis=new FileInputStream("e:\\1.txt");
DataInputStream dis=new DataInputStream(fis);
FileOutputStream fos=new FileOutputStream("e:\\2.txt");
DataOutputStream dos=new DataOutputStream(fos);
short buf;
while((buf=dis.readShort())!=-1){
dos.writeInt((char)buf);
}
dis.close();
dos.close();
}
}
package com.test;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

class Demo2 {
void demo1() throws IOException{
FileInputStream fis=new FileInputStream("e:\\2.txt");
DataInputStream dis=new DataInputStream(fis);
FileOutputStream fos=new FileOutputStream("e:\\3.txt");
DataOutputStream dos=new DataOutputStream(fos);
char buf;
while((buf=dis.readChar())!=-1){
dos.writeShort((short)buf);
}
dis.close();
dos.close();
}
}
1.txt:
dlsjdfl;adjlsfj计量检定浪费ddsfdjlsfj;sa11
2.txt:
dl sj df l; ad jl sf j? 屏 考 於 ɡ 朔 裠 ds fd jl sf j; sa 11
3.txt:
dl sj df l; ad jl sf j? 屏 考 於 ɡ 朔 裠 ds fd jl sf j; sa 11
为何会这样那?
...全文
165 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cjbkemp 2014-05-13
  • 打赏
  • 举报
回复
已ok,谢谢各位!
alan19931103 2014-05-11
  • 打赏
  • 举报
回复
不好意思,不想看。又不排版,而且估计也不会来结贴。我就是来蹭分的。睡觉
乔不思 2014-05-11
  • 打赏
  • 举报
回复
默认的话是 操作系统的字符编码,但是记事本的话,默认的是utf-8,所以出现了乱码现象, 解决方式两种:1.把记事本的数据另存为,设置字符编码为GBK, 2.在输出流上设置字符编码为 utf-8. 二选一即可。。。应该就可以
乔不思 2014-05-11
  • 打赏
  • 举报
回复
乱码了,设置一下流的字符集编码。。。

62,620

社区成员

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

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