java读写二进制文件的解决方法
程序员文章站
2023-12-12 15:51:34
接口:writerable复制代码 代码如下:package com.geoway.pad.common; import java.io.datainput; import...
接口: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接口时写的,并没有在项目中使用,也不知道在项目中这样用会不会有什么问题。
复制代码 代码如下:
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接口时写的,并没有在项目中使用,也不知道在项目中这样用会不会有什么问题。