IO体系结构及简单应用

[size=large]IO体系结构

Java将输入输出抽象为流,与之相关的类都在java.io包中。

流按方向分为输入流InputStream和输出流OutputStream.程序使用输入流对象从数据源读取数据,使用输出流对象向目的地写出数据。

常用的流有:
基础字节流(原始流):
InputStream,OutputStream(都是抽象类)是java中可以按最小数据单位读取
的流,即每次读写一个字节,基础流是直接连接到输入源的流。
过滤流(节点流):
过滤流是用来包装基础流以提供更好的特性,如提供缓冲功能的
BufferedInputStream和BufferedOutputStream,它并不直接连接到数据源。
基于具体数据类型的流:
要从流中读取指定数据类型的数据时,用到DataInputStream,DataOutputStream
基于对象读写:
对象的输入输出流ObjectInput,ObjectOutput.对象流的读取又叫java对象序
列化技术。

一、
InputStream中的重要方法:
int available() 流中可以读取的有效字节长度(多少个byte)
void close() 流对象使用完后要关闭,就像水龙头,用完要关,否则会占用系统资源
int read() 这个方法调用会返回流中的下一个字节作为一个byte值,如果已经读到末尾
则返回-1,表示流中数据已经读完。次方法返回虽为int,但读取的是
一个byte,如果要读取一个int型,那么要用到DataInput对象的
readInt()方法
int read(byte[] b) 用从流中读到的byte

OutputStream中的重要方法:
void close() 关闭流
void flush() 将输出流有可能还保存在JVM内存中的数据强制输出到目标中
void write(byte[] b) 将byte数组中的内容输出到流中
void write(byte[] b,int off,int len) 将数组中的一部分写到流中
void write(int b) 向流中写入一个byte值!如果要写入一个int型,则要使用
DataOutput对象的writeInt()fangfa

读取文件举例:[/size]

/**
* 读取文件的方法
* @throws IOException
*/
public String readFile(String path) throws IOException{
FileInputStream is = new FileInputStream(path);

int length = is.available();
byte[] array = new byte[length];
for(int i=0;i<length;i++){
array[i] = (byte) is.read();
}
//把byte类型的数组转换成String类型
String str = new String(array);
System.out.println("读取文件:"+str);
is.close();
return str;
}


[size=large]二、
缓冲流是一种过滤流。常用的是BufferedInputStream,BufferedOutputStream

之前的流读写文件是如下过程:
输入文件->操作系统内存->JVM内存->代码变量->JVM内存->操作系统内存->输出文件。
经历这样的一个过程才传输了一个字节,这样就造成了效率低下速度缓慢;
而缓冲流机制是在JVM开辟一定大小的缓冲区(目前8M),当输入缓冲区的数据达到一定值时就一次性输出到文件中。这样很大程度上节省了时间。

使用缓冲流时注意:
缓冲区不满时是不会自动输出到文件中的,需要添加关闭命令,如果文件很大,缓冲区满了则会自动输出并清空缓冲区继续读取!所以为保证完整输出,需要关闭Buffered流。

构造器:
BufferedInputStream(InputStream in)
使用一个InputStream的输入流对象创建一个默认缓冲区大小(8M)的缓冲输入流对象
BufferedInputStream(InputStream in,int size)指定缓冲区大小

BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStream out,int size)[/size]


/**
* BufferedInputStream和BufferedOutputStream完成文件的复制
* @author 客
*
*/
public class BufferedStream {

/**
* 主方法
* @param
* @throws IOException
*/
public static void main(String[] args) throws IOException{
BufferedStream bs = new BufferedStream();
//创建两个路径分别表示被复制文件路径和新文件路径
String path = "src\\lwq\\java\\File_MyExercise0712\\BufferedStream.java";
String newPath = "src\\lwq\\java\\File_MyExercise0712\\BufferedStream.java.bak";
bs.copyFile(path,newPath);
}
/**
* 完成复制的方法
* @throws IOException
*/
public void copyFile(String path,String newPath) throws IOException{
FileInputStream is = new FileInputStream(path);
BufferedInputStream bis = new BufferedInputStream(is);

FileOutputStream os = new FileOutputStream(newPath);
BufferedOutputStream bos = new BufferedOutputStream(os);

int length = is.available();
byte[] array = new byte[length];

for(int i=0;i<length;i++){
array[i] = (byte)bis.read();
}
bos.write(array);
bos.close();
}
}


[size=large]三、
串行化又叫做对象序列化,是可以通过流对象保存java对象的一种方法。在java中要事项序列化的对象的类,必须事项java.io.serializable接口,未实现此接口的类将无法使其任何状态序列化或反序列化。序列化接口没有方法或字段,仅用于标识可序列化的语义。

对象类所表示的数据在不断改变,所以他们不会被串行化,如java.io.FileInputStream,
java.io.FileOutputStream和java.lang.Thread等流。

有时候为了保密需要,类不允许自己对象的某个属性被序列化,则可以再属性前加transient关键字,那么该属性在对象的保存时不会生效,读取时也不会得到数据。

对象读写,一般使用java.io.ObjectInputStream类的void writeObject(Object o)方法向流中写入一个对象和java.io.ObjectOutputStream类的Object readObject()读取一个对象。

四、
DataInputStream和DataOutputStream主要用来读写指定的数据类型。


五、画图板文件的保存打开
电脑中存储的不同信息有不同的存储格式。每一种文件格式通常有一种或多种扩展名,但也可以没有扩展名,扩展名对文件本身不会有影响,只是标示打开的优先方式。。

文件由两部分组成:
1、文件头信息:包括存储类型等文件的整体信息;
2、文件内容:具体的。

以画图板为例,头部信息包括图片的存储类型,图片的宽和高,起始位置;
文件内容记录每一个像素点的颜色值。

在文件保存时首先要对画板图片部分进行截屏操作,并保存图片头信息和内容[/size]

/**
* 截取画板上图片的类
* @author 客
*
*/
public class ScreenCapture {

public int[][] createScreenCapture(JPanel drawPanel) throws AWTException{
//得到画图面板的高度
int height = drawPanel.getHeight();
//得到画图面板的宽度
int width = drawPanel.getWidth();
System.out.println("height="+height+" width="+width);

//获取画图面板在屏幕的位置
Point point = drawPanel.getLocationOnScreen();
System.out.println("x="+point.getX()+" y="+point.getY());

//实例化一个矩形对象
Rectangle rect = new Rectangle((int)point.getX(),(int)point.getY(),width,height);

//实例化一个机器人类的对象
Robot robot = new Robot();

//开始截取屏幕上的图片
BufferedImage image = robot.createScreenCapture(rect);

//实例化一个二维数组对象,用来存储图片每个像素点的值
int[][] array = new int[width][height];

//遍历图片,将每个像素点的颜色取出,存入到数组中
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
array[i][j] = image.getRGB(i, j);
}
}
return array;
}
}


[size=large]在点击保存和打开时,对于截屏时的内容进行写入文件和读取操作。[/size]

/**
* 画图程序的保存和打开操作
* @author 客
*
*/
public class OpenSave {

/**
* 文件保存的方法
* @param path,传入的保存路径
* @param array,将图片内容保存到数组中
* @return
*/
public boolean save(String path,int[][] array){
boolean b = false;
try {
FileOutputStream os = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);

/**
* 文件的格式:1、文件头:文件类型,图片宽度,图片高度
* 2、文件内容:每个像素点的颜色值
*/
//图片宽度
dos.writeInt(array.length);
//图片高度
dos.writeInt(array[0].length);
//写文件内容
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
dos.writeInt(array[i][j]);
}
}
b = true;

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}

/**
* 打开文件的方法
* @param path
* @return
*/
public int[][] open(String path){
int[][] array = null;
try {
FileInputStream is = new FileInputStream(path);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);

//读图片的宽度,高度
int width = dis.readInt();
int height = dis.readInt();
System.out.println("width="+width+" height="+height);

array = new int[width][height];
//写文件内容
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
array[i][j] = dis.readInt();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return array;
}
}



如有不足欢迎指正! :D
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值