日期:2014-05-20  浏览次数:20951 次

输出乱码
package ding;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class fff 
{
 public static void main(String[] args) throws Exception 
 {
write();
read();
}
 public static void read() throws Exception 
 {
File kFile=new File("e:"+File.separator,"1.dat");
DataInputStream jj=new DataInputStream(new BufferedInputStream(new FileInputStream(kFile)));
System.out.print(jj.readDouble());


int i=(int)kFile.length()-8;
byte[] h=new byte[i];
jj.read(h);
byte[] temp=new byte[h.length];
int j=0;
for (byte iter :h)
{
if(iter!='\0')
{
temp[j]=iter;
j++;
}
}
System.out.print(new String(temp));

}
 public static void write() throws IOException 
 {
File file=new File("e:"+File.separator,"1.dat");
if(!file.exists()){file.createNewFile();}
DataOutputStream jj=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file,true)));
jj.writeDouble(1.4444);
Scanner sc=new Scanner(System.in);
String kkString;
while (!(kkString=sc.nextLine()).equals("y"))
{
jj.write(kkString.getBytes());
}
jj.close();
}

}


------解决方案--------------------
LZ去看一下FileInputStream和FileReader的区别,应该可以使用FileReader来解决问题,一个是通过单字节来读取文件内容,一个是通过双字节来读取内容,如果你文件里面含有中文的话,建议使用FileReader就可以了