初学者询问一个关于datainputstream的有关问题
菜鸟询问一个关于datainputstream的问题
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
为何会这样那?
------解决方案--------------------
默认的话是 操作系统的字符编码,但是记事本的话,默认的是utf-8,所以出现了乱码现象,
解决方式两种:1.把记事本的数据另存为,设置字符编码为GBK,
2.在输出流上设置字符编码为 utf-8.
二选一即可。。。应该就可以
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
为何会这样那?
------解决方案--------------------
默认的话是 操作系统的字符编码,但是记事本的话,默认的是utf-8,所以出现了乱码现象,
解决方式两种:1.把记事本的数据另存为,设置字符编码为GBK,
2.在输出流上设置字符编码为 utf-8.
二选一即可。。。应该就可以