java读写二进制文件的解决方法

所属分类: 软件编程 / java 阅读数: 59
收藏 0 赞 0 分享
接口:Writerable
复制代码 代码如下:

package com.geoway.pad.common;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public interface Writerable {
       //write 
       public void  write(DataOutput data) throws IOException;
       //read
       public  void read(DataInput di) throws IOException;
}

接口实现类:UserWriter
复制代码 代码如下:

package com.geoway.pad.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

public class UserWriter implements Writerable{
    //getter  setter  methods
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public boolean isAdministrator() {
        return administrator;
    }
    public void setAdministrator(boolean administrator) {
        this.administrator = administrator;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getPassword() {
        return password;
    }
    public void setText(String text) {
        this.text = text;
    }
    public String getText() {
        return text;
    }
    public void setImage(byte[] image) {
        this.image = image;
    }
    public byte[] getImage() {
        return image;
    };
    //private  fields
     private int number;
     private String id;
     private int age;
     private boolean administrator;
     private  String userName;
     private  String password;
     private  String text;
     private  byte[]  image;

    @Override
    public void write(DataOutput data) throws IOException {
       data.writeInt(number);
       data.writeUTF(id);
       data.writeInt(age);
       data.writeBoolean(administrator);
       data.writeUTF(userName);
       data.writeUTF(password);
       data.writeUTF(text);
       data.write(image);

    }
    @Override
    public void read(DataInput di) throws IOException {
       this.number=di.readInt();
       this.id=di.readUTF();
       this.age=di.readInt();
       this.administrator=di.readBoolean();
       this.userName=di.readUTF();
       this.password=di.readUTF();
       this.text=di.readUTF();
       //读取图片
       this.image=new byte[2048];
       try{
         di.readFully(this.image);
       }catch(Exception e){
           System.out.println(e.toString());
       }
    }
  public static byte[] in2byte(InputStream is) throws IOException{  
     byte[] bs= new byte[1024];  
     ByteArrayOutputStream bos = new ByteArrayOutputStream();  
     int len = -1;  
     while((len=is.read(bs))!=-1){  
         bos.write(bs,0,len);  
     }  
     bs = bos.toByteArray() ;  
     return bs ;  
   }  
   public static  void copy(InputStream in, OutputStream out) throws IOException {  
       byte[] buf = new byte[1024];  
       while (true) {  
                int len = in.read(buf);  
                   if (len < 0) break;  
          out.write(buf, 0, len);  
          }  
  }  
 public  static  void main(String[] args) throws FileNotFoundException, IOException{
        UserWriter user=new UserWriter();
        user.setUserName("likehua");
        user.setPassword("password");
        user.setId(UUID.randomUUID().toString());
        user.setNumber(123);
        user.setAdministrator(false);
        //加一段文字
        user.setText("做一个天气预报,可以保存要显示的城市名称 在增加城市页面,我的rms关键操作如下(其中SetCanvas.cityName是一个Vector,用来保存城市名,在执行此段代码之前,我已经把新增加的城市名添加进了这个Vector ");
        //传入一张图片
        user.setImage(in2byte(new FileInputStream("c:\\1.jpg")));
        user.write(new DataOutputStream(new FileOutputStream("c:\\user.data")));
        System.out.println("完毕");       
        System.out.println("....读取开始...");
        UserWriter u=new UserWriter();
        u.read(new DataInputStream(new FileInputStream("c:\\user.data")));
        System.out.println(u.getUserName());
        System.out.println(u.getPassword());
        System.out.println(u.isAdministrator());
        System.out.println(u.getText());
        //拿出图片
        copy(new ByteArrayInputStream(u.getImage()), new FileOutputStream("c:\\copy.jpg"));

    }

}

本例只是本人测试DataOutput和DataInput接口时写的,并没有在项目中使用,也不知道在项目中这样用会不会有什么问题。
更多精彩内容其他人还在看

Javaweb 鼠标移入移出表格颜色变化的实现

这篇文章主要介绍了Javaweb 鼠标移入移出表格颜色变化的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Java 实现图片压缩的两种方法

这篇文章主要介绍了Java 实现图片压缩的两种方法,帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

据说这个是可以撸到2089年的idea2020.2(推荐)

这篇文章主要介绍了据说这个是可以撸到2089年的idea2020.2,本教程给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

一篇文章带你搞定SpringBoot不重启项目实现修改静态资源

这篇文章主要介绍了一篇文章带你搞定SpringBoot不重启项目实现修改静态资源,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

win10操作系统下重启电脑java环境变量失效

这篇文章主要介绍了win10操作系统下重启电脑java环境变量失效,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Java实现批量修改文件名和重命名的方法

这篇文章主要介绍了Java实现批量修改文件名和重命名的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

关于Java HashMap自动排序的简单剖析

这篇文章主要给大家介绍了关于Java HashMap自动排序的简单剖析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring中BeanFactory和ApplicationContext的作用和区别(推荐)

这篇文章主要介绍了Spring中BeanFactory和ApplicationContext的作用和区别,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java程序执行Cmd指令所遇问题记录及解决方案

这篇文章主要介绍了Java程序执行Cmd指令所遇问题记录,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

深入浅析jni中的java接口使用

这篇文章主要介绍了jni中的java接口使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多