package bittable;
import java.io.*;
public class BinaryFile {
  public  static byte[] read(File file) throws IOException{
  BufferedInputStream bf=new BufferedInputStream(new FileInputStream(file));
  try{
  byte[] data=new byte[bf.available()];
  bf.read(data);
  return data;
  }finally{
  bf.close();
 }
  } 
  public static  byte[]
     read(String file)throws IOException{
  return read(new File(file).getAbsoluteFile());
  }
  public static void main(String[] args) {
 try{
  BinaryFile b=new BinaryFile();
  
  byte[] bb=b.read("123.dat");
  System.out.println(new String(bb));
 }catch(IOException e){
 throw new RuntimeException(e);
 }

 System.out.println();
 }
 
}
二进制文件123.dat的内容是1 2 3 4 5 6 7 8 9 10
运行程序bb=[49, 32, 50, 32, 51, 32, 52, 32, 53, 32, 54, 32, 55, 32, 56, 32, 57, 32, 49, 48, 13, 10]
其中10被处理成49,48
我想问这是二进制的输出吗?
如果是同样的文本文件,10又输出什么?

解决方案 »

  1.   

    123.dat不是二进制文件吧,是文本文件.
    你读到的是字符的uncord,
    0,1,2....9的uncord是48,49,...57,空格的uncord是32最后的\n\r分别是13 ,10
      

  2.   

    1 对应的ASCII码为 49
    你按字节的方式读出来,那么读出来的就是一个个ASCII码
    txt文件,不知道你是想用什么方式读,如果用readLine读的话
    那就是1 2 3 4 5 6 7 8 9 10
      

  3.   

    二进制文件,你要用DataOutputStream或者是RandomAccessFile来输出你的数据.再用DataInputStream或RandomAccessFile读出.
      

  4.   

    Integer.toBinaryString(int i)