【转】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 ...
随机推荐
- Effective C++ 第二版 40)分层 41)继承和模板 42)私有继承
条款40 通过分层来体现"有一个"或"用...来实现" 使某个类的对象成为另一个类的数据成员, 实现将一个类构筑在另一个类之上, 这个过程称为 分层Layeri ...
- JNI- java.lang.UnsatisfiedLinkError: Native method not found
http://stackoverflow.com/questions/24566127/jni-java-lang-unsatisfiedlinkerror-native-method-not-fou ...
- Struts2框架具体解释
在Struts2的Model-View-Controller模式实现下面五个核心组件: 动作-Actions 拦截器-Interceptors 值栈/OGNL 结果/结果类型 视图技术 Struts ...
- Filtering Specific Columns with cut
Filtering Specific Columns with cut When working with text files, it can be useful to filter out s ...
- poj 1988 Cube Stacking && codevs 1540 银河英雄传说(加权并茶几)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 30010 using nam ...
- Web Api学习一
接触WebApi读的第一篇文章: ASP.NET Web API(一):使用初探,GET和POST数据 实践过程中,用的Fiddler模拟Post请求时收到的对象总是为空null 解决:将文章中的内容 ...
- Html.Action、html.ActionLink与Url.Action的区别
1.html.ActionLink返回的指向指定controller.指定action的超链接标签<a>标签.如果没有指定controller,则默认为本页面对应的Controller. ...
- Android学习手记(4) BroadcastReceiver监听电池信息
Android 中,Broadcast是一种在应用程序之间进行传输信息的机制.BroadcastReceiver对发送过来的Broadcast进行过滤和响应.根据这种机制,我们可以获取电池现有电量等信 ...
- 28 Corn表达式详解 (转自http://blog.csdn.net/claram/article/details/51785193)
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...
- oracle单行函数之类型转换
oracle数据类型转换:显示转换盒隐式转换 oracle自动完成转换