您的位置:首页 > 编程语言 > Java开发

JAVA输入/输出系统中的其他流学习笔记

2014-06-14 17:42 537 查看
一、字节数组流

  字节数组流类能够操作内存中的字节数组,它的数据是一个字节数组。字节数组流类本身适配器设计模式,它把字节数组类型转为流类型使得程序能够对字节数组进行读写操作。

1.ByteArrayInputStream类

  ByteArrayInputStream类从内存中的字节数组读入数据,它的数据是一个字节数组。ByteArrayInputStream流类本身适配器设计模式,它把字节数组类型转换为输入流类型使得程序能够对字节数组进行读操作。

下面是示例代码:

public class ByteArrayInputStreamDemo {

public static void main(String args[]) throws IOException{

readByteArrayStream();
}

/***
* ByteArrayInputStream读取字节数组的方法
* @throws IOException
*/
public static void readByteArrayStream() throws IOException{

String str="zhisytoitheima";
byte[] strBuf=str.getBytes();
ByteArrayInputStream baiS=new ByteArrayInputStream(strBuf);
int data=baiS.read();
while(data!=-1){
char upper=Character.toUpperCase((char)data);
System.out.print(upper+" ");
data=baiS.read();
}
baiS.close();
}
  }


运行结果如下:



2.ByteArrayOutputStream类

  ByteArrayOutputStream类向内存中的字节数组写入数据,它的数据是一个字节数组。ByteArrayOutputStream流类本身适配器设计模式,它把字节数组类型转换为输出流类型使得程序能够对字节数组进行写操作。

  ByteArrayOutputStream类的构造方法如下:

ByteArrayOutputStream():创建一个新的字节数组输出流,缓冲区的初始是32个字节,如有必要可增加大小。

ByteArrayOutputStream(int size):创建一个新的字节数组输出流,它具有指定大小的缓冲区容量,容量以字节为单位。

代码示例:

/**
* 把字符串转换为字节数组再写到字节数组输出流中
* @author 支胜勇
*
*/
public class ByteArrayOutputStreamDemo {

public static void main(String args[]) throws IOException{

writeByteArrayStream();
}

/***
* ByteArrayInputStream读取字节数组的方法
* @throws IOException
*/
public static void writeByteArrayStream() throws IOException{

String str="zhisytoitheimatestByteArrayInputStream";
byte[] strBuf=str.getBytes();
ByteArrayOutputStream baoS=new ByteArrayOutputStream(512);

baoS.write(strBuf);//将指定的字节数组中的字节写入baoS字节数组输出流中
System.out.println(baoS.toString().toUpperCase());//通过解码字节将缓冲区内容转换为字符串输出

//创建一个行新分配的字节数组,并将字节数组输出流中的内容复制到该数组中
byte[] newStrBuf=baoS.toByteArray();
for(int i=0;i<newStrBuf.length;i++){
System.out.print((char)newStrBuf[i]);
}

}
  }




二、管道流

  一个PipedInputStream对象必须和一个PipedOutputStream对象进行连接从而产生一个通信管道。PipedOutputStream可以向管道中写入数据,通常一个线程向管道输出流写入数据,另一个线程从管道输入流中读取数据,PipedInputStream可以从管道中读取PipedOutputStream写入的数据。这两个类主要用来完成线程之间的通信。当线程A执行管道输入流的read()方法时,如果暂时还没有数据,则这个线程就会被阻塞,只有当线程B向管道输出流谢了新的数据后,线程A才会被回复运行。

示例代码:

  /**
* 向管道输输出流写入数据的线程
*/
class MyWriter extends Thread{

private PipedOutputStream out=new PipedOutputStream();
private String content="";

public MyWriter(String content){

this.content=content;
}

public PipedOutputStream getOut() {
return out;
}

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try{
System.out.println("--------我开始向管道写入数据------");
out.write(content.getBytes());
out.close();
System.out.println("--------向管道写入数据结束------");
}catch(Exception e){

throw new RuntimeException(e);
}
}

}

/***
* 向管道输入流中读取数据
*/
public class PipedStreamDemo extends Thread{

private PipedInputStream in;

public PipedStreamDemo(MyWriter out) throws IOException{

this.in=new PipedInputStream();
in.connect(out.getOut());//连接管道输出流和管道输入流
}

@Override
public void run() {
// TODO Auto-generated method stub
super.run();

try{
byte[] buf=new byte[1024];
int len=0;
String readStr="";
System.out.println("-------我在等待向管道写入数据!--------");
while((len=in.read(buf))!=-1){

readStr+=new String(buf,0,len);
}
System.out.println(readStr);
in.close();
System.out.println("-------我从管道读取数据结束!--------");
}catch(Exception e){

throw new RuntimeException(e);
}
}

public static void main(String args[]) throws IOException{

MyWriter myW=new MyWriter("我很想到黑马去!");

PipedStreamDemo myR=new PipedStreamDemo(myW);
myW.start();
myR.start();
}

      }




三、随机访问文件类

  在Java中,RandomAccessFile类的一个对象提供了对随机读/写文件的支持,它没有继承InputStream和OutputStream,而是直接继承了Object,并且实现了接口DataInput和DataOutput。在生成一个RandomAccessFile对象时,除了要指明文件对象或文件名外,还需要指明读/写模式。例如下面的语句:

RandomAccessFile raf=new RandomAccessFile(“E:\\yy.dat”,”r”);表示对文件E:\\yy.dat进行随机读操作

RandomAccessFile raf=new RandomAccessFile(“E:\\yy1.dat”,”rw”);表示对文件E:\\yy1.dat进行随机读/写操作

  可讲Java随机读/写的字节文件看成是一个很大的字节数组,随机读/写操作可看成是对这个“虚拟的字节数组”进行的,这个“数组”的下标就是所谓的文件指针,因此随机读/写文件的首要操作就是移动文件的指针,其操作有以下三个:

long getFilePointer():当前文件的指针位置。

void seek(long pos):移动文件指针到指定的位置,从0开始计算位置。

int skipBytes(int n):将文件指针向文件末尾移动指定的n个字节,返回实际移动的字节数,若n<0则不发生移动。

示例代码:

/**
* 向e:\\yy.txt文件写入10个double型的实数,然后运用RandomAccessFile随机修改其中的数据
* @author 支胜勇
*
*/
public class RandomRWDemo {

public static void main(String[] args) throws IOException{

randomWrite("E:\\yy.dat");

randomModify("E:\\yy.dat");
}

/***
* 向文件写入数据
* @param fileName
* @throws IOException
*/
public static void randomWrite(String fileName) throws IOException{

File file=new File(fileName);
if(!file.exists()){

file.createNewFile();
}

if(!file.isFile()){//如果不是文件则返回

System.out.print("不是文件");
return;
}

RandomAccessFile aWFile=new RandomAccessFile(file,"rw");
int i=1;
while(i<=10){
aWFile.writeDouble(i);//写入doubl值
System.out.print(" "+(double)i);
i++;
}
aWFile.close();
}

public static void randomModify(String fileName) throws IOException{

final int DOUBLE_SIZE=8;//double类型占8个字节
File file=new File(fileName);
if(!file.exists()){
System.out.println("要修改的文件不存在");
return;
}

if(!file.isFile()){
System.out.println("不是文件");
return;
}

RandomAccessFile aMFile=new RandomAccessFile(file,"rw");

aMFile.seek(2*DOUBLE_SIZE);//修改第2个double值
aMFile.writeDouble(100);

aMFile.seek(7*DOUBLE_SIZE);//修改第7个double值
aMFile.writeDouble(200);

aMFile.close();

//查看是否已修改

RandomAccessFile aRFile=new RandomAccessFile(file,"r");
System.out.println();
for(int i=0;i<10;i++){
System.out.print(" "+aRFile.readDouble());
}
aRFile.close();
}

  }


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: