【转】java读写二进制文件的解决方法
原文网址:http://www.jb51.net/article/36940.htm
接口:Writerable
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
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接口时写的,并没有在项目中使用,也不知道在项目中这样用会不会有什么问题。
【转】java读写二进制文件的解决方法的更多相关文章
- Tomcat 优化 java.lang.OutOfMemoryError: Java heap space 的解决方法
Tomcat 优化 java.lang.OutOfMemoryError: Java heap space 的解决方法 java.lang.OutOfMemoryError: Java heap sp ...
- java.lang.ClassNotFoundException的解决方法
java.lang.ClassNotFoundException的解决方法 出现这个问题的原因可能很多,但是最终原因都是部署的项目文件中没有这个类包. 那么出错的点在哪呢?逐一排除! 1.首先在项 ...
- Tomcat部署java项目java.lang.OutOfMemoryError异常解决方法
java.lang.OutOfMemoryError异常解决方法 Window系统环境下,在catalina.bat文件第一行添加以下内容 set JAVA_OPTS=-Xms512m -Xmx512 ...
- 对于jfinal中java.lang.Long cannot be cast to java.lang.Integer的解决方法
@Jfinal 老大提供的解决方法 当数据库字段为 int 型(有符号int型),但是如果在 sql 中使用了某些函数,jdbc 会自动转型为 long,例如:select sum(money) fr ...
- android 工程里缺少 R.java 文件原因和解决方法
作为新手,学习android 的时候难免要导入一些示例,目的为了更加了解android各种API用法,顺便也可以学习下别人代码的写法. 可是导入android源码后,基本都有错误,R.java也不会自 ...
- Java中线程出现Exception in thread "Thread-0" java.lang.IllegalMonitorStateException异常 解决方法
代码 package thread; public class TestChongNeng { public static void main(String[] args) { Thread t1 = ...
- golang中map并发读写问题及解决方法
一.map并发读写问题 如果map由多协程同时读和写就会出现 fatal error:concurrent map read and map write的错误 如下代码很容易就出现map并发读写问题 ...
- (转)Android工程出现 java.lang.NoClassDefFoundError错误解决方法
在Eclipse中,导入Android工程,工程没有报错,运行时,出现 java.lang.NoClassDefFoundError类没有找到的错误.从问题上可以看出是导入包出错的原因.遂百度加谷歌. ...
- Java小问题的解决方法系列
1)IDEA中文乱码,解决方法:http://blog.csdn.net/zht666/article/details/8953516 2)卸载OpenJdk,http://my.oschina.ne ...
随机推荐
- input上传文件显示图片缩略图
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Hadoop32位和64位的查询
1.查看自己的hadoop版本是32位还是64位 进入: hadoop-2.6.4/lib/native 使用file命令 file libhadoop.so.1.0.0
- checkbox遍历操作, 提交所有选中项的值
<div class="content_list pad_10 hidden" > <h3>修改可配送地区</h3> <input typ ...
- JNI 详细使用步骤 上手示例
1.定义本地native方法 定义本地方法,通常情况下,应单独定义一个类来封装所有native方法 /** 存放native方法的类 */ public class MyNativeMethods { ...
- android系统360浏览器使用append动态添加元素出现黑色错乱背景
去掉样式 backface-visibility:hidden;
- Ajax调用webService(一) 不跨域。
注:需要的js文件与组件(jquery-1.4.2.min.js和Newtonsoft.Json) 同域:要调用的webservice与ajax请求页面在同一个网站下(本文中都是本地测试). 数据库( ...
- WCF调用问题
1---WCF常用的节点绑定 basicHttpBinding和netTcpBinding 当通过Spring对象注入 如 WCFForOrderService 对象注入 public WCFForO ...
- 安装VS2010后,如何设置老版本的项目文件不是默认用VS2010打开
1.系统先后安装了VS2008和VS2010,在打开用VS2008创建的项目文件时总是会默认用VS2010打开,选择打开方式都不行,很不方便,差点要把VS2010卸载了. 其实只需要简单设置V ...
- oralce中exists not exists in not in对于NULL的处理
1. 先讨论 in 与 not in中存在NULL的情况, sql语句如下: 1 select 1 result1 from dual where 1 not in (2, 3); 2 3 4 s ...
- HTML5 Canvas 中的颜色、样式和阴影的属性和方法
颜色.样式和阴影的属性与方法 fillStyle 设置或返回用于填充绘画的颜色.渐变或模式 strokeStyle 设置或返回用于笔触的颜色.渐变或模式 ...