使用Java操作二进制文件 示例

/*当我们对二进制的文件处理的时候,我们应该使用FileInputStream和FileOutputStream。

当我们操作文件的时候,可以首先使用File类得到对这个文件的引用,例如
File file = new File("Idea.jpg");然后把file作为参数传给FileInputStream或者FileOutputStream得到相应的输入流或者输出流。通常我们对文件无非是进行读写,修改,删除等操作。最重要的就是读写操作。当我们读文件的时候应该使用InputStream,写文件的时候使用OutputStream。read()方法是在InputStream中定义的,它是个抽象方法。InputStream当然也是个抽象类,我们得到的实例都是它的子类,例如FileInputStream,子类如果不是抽象类的话就要实现父类的抽象方法。在FileInputStream中不但实现了read()并且重载了这个方法提供了read(byte[] buffer)和read(byte[] buffer,int off,int length)两个方法。下面详细介绍一下:

    read()方法将读取输入流中的下一个字节,并把它作为返回值。返回值在0-255之间,如果返回为-1那么表示到了文件结尾。用read()我们可以一个一个字节的读取并根据返回值进行判断处理。
while((ch = image.read())!=-1)
{
     System.out.print(ch);
     newFile.write(ch);
}

    read(byte[] buffer)会把流中一定长度的字节读入buffer中,返回值为实际读入buffer的字节长度,如果返回-1表示已经到了流的末尾。
 while((ch = image.read(buffer))!=-1)
{
     System.out.println(ch);
     newFile.write(buffer);
}

    read(byte[] buffer,int off,int length)的意思是把流内length长度的字节写入以off为偏移量的buffer内,例如off=7,length=100的情况下,这个方法会从流中读100个字节放到buffer[7]到buffer[106]内。返回值为实际写入buffer的字节长度。
 while((ch = image.read(buffer,10,500))!=-1)
{
     System.out.println(ch);
     newFile.write(buffer,10,500);

}

    对上面的方法进行介绍的时候我们没有考虑异常的情况,读者应该参考API doc进行必要的了解。当我们对流操作的时候,有的时候我们可以对流进行标记和重置的操作,当然要流支持这样的操作。参考一下mark(),reset()和markSupported()方法的说明。最后在使用结束后,确保关闭流,调用close()方法。由于FileOutputStream的write相关的方法和FileInptutStream的read()非常类似,因此不再多说。下面提供一个例子说明如何对二进制文件进行操作,我们打开一个JPEG格式的文件,通过三种不同的方式读取内容,并生成一个新的文件。运行结束后你会发现这两个文件完全一样!

*/

import java.io.*;
public class LinkFile{
 public static void main(String[] args) throws IOException{
  linkBinaryFile("chj.jpg");
 }
 private static void linkBinaryFile(String fileName) throws IOException{
  File imageFile = new File(fileName);
  if(!imageFile.exists()&&!imageFile.canRead()){
   System.out.println("can not read the image or the image file doesn't exists");
   System.exit(1);
  }
   long length = imageFile.length();
   int ch = 0;
   System.out.println(length);
   byte[] buffer = new byte[(int)length/7];
   InputStream image = new FileInputStream(imageFile);
 
   File file = new File("hello.jpg");
   if(!file.exists()){
  file.createNewFile();
   }
   FileOutputStream newFile = new FileOutputStream(file,true);
 
   boolean go = true;
   while(go){
     System.out.println("please select how to read the file:\n"+
     "1: read()\n2:read(byte[] buffer)\n3:read(byte[] buffer,int off,int len)\n");
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     String line = br.readLine();
     if(line.equals("1")){
       while((ch = image.read())!=-1){
     System.out.print(ch);
     newFile.write(ch);
    }
    }else if(line.equals("2")){
   while((ch = image.read(buffer))!=-1){
        System.out.println(ch);
    newFile.write(buffer);
   }
  }else if(line.equals("3")){
   while((ch = image.read(buffer,10,500))!=-1){
    System.out.println(ch);
    newFile.write(buffer,10,500);
   }
   for(int i = 0;i<10;i++){
   System.out.print(buffer[i]);
   }
  }
  go = false;
 }
 image.close();
 newFile.close();
 }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值