国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

java讀寫(xiě)二進(jìn)制文件的解決方法

2019-11-26 16:06:26
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
接口:Writerable
復(fù)制代碼 代碼如下:

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;
}

接口實(shí)現(xiàn)類(lèi):UserWriter
復(fù)制代碼 代碼如下:

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("做一個(gè)天氣預(yù)報(bào),可以保存要顯示的城市名稱(chēng) 在增加城市頁(yè)面,我的rms關(guān)鍵操作如下(其中SetCanvas.cityName是一個(gè)Vector,用來(lái)保存城市名,在執(zhí)行此段代碼之前,我已經(jīng)把新增加的城市名添加進(jìn)了這個(gè)Vector ");
        //傳入一張圖片
        user.setImage(in2byte(new FileInputStream("c://1.jpg")));
        user.write(new DataOutputStream(new FileOutputStream("c://user.data")));
        System.out.println("完畢");       
        System.out.println("....讀取開(kāi)始...");
        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"));

    }

}

本例只是本人測(cè)試DataOutput和DataInput接口時(shí)寫(xiě)的,并沒(méi)有在項(xiàng)目中使用,也不知道在項(xiàng)目中這樣用會(huì)不會(huì)有什么問(wèn)題。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁夏| 石泉县| 修武县| 墨脱县| 曲沃县| 会昌县| 鹤岗市| 保德县| 赞皇县| 定结县| 十堰市| 怀柔区| 仁怀市| 航空| 郎溪县| 康乐县| 武平县| 宁南县| 博罗县| 平昌县| 临夏县| 罗城| 容城县| 故城县| 西乌珠穆沁旗| 兰西县| 赤峰市| 壶关县| 湘潭县| 锡林郭勒盟| 大安市| 余庆县| 江城| 沈丘县| 无为县| 双流县| 雅安市| 仁布县| 福鼎市| 五家渠市| 东城区|