java

位置:IT落伍者 >> java >> 浏览文章

彻底明白Java的IO系统


发布日期:2024年01月28日
 
彻底明白Java的IO系统

彻底明白Java的IO系统caiyi(收藏)

关键字 JavaIO系统

一. Input和Output

stream代表的是任何有能力产出数据的数据源或是任何有能力接收数据的接收源在Java的IO中所有的stream(包括Input和Out stream)都包括两种类型

以字节为导向的stream

以字节为导向的stream表示以字节为单位从stream中读取或往stream中写入信息以字节为导向的stream包括下面几种类型

) inputstream

) ByteArrayInputStream把内存中的一个缓沖区作为InputStream使用

) StringBufferInputStream把一个String对象作为InputStream

) FileInputStream把一个文件作为InputStream实现对文件的读取操作

) PipedInputStream实现了pipe的概念主要在线程中使用

) SequenceInputStream把多个InputStream合并为一个InputStream

) Outstream

) ByteArrayOutputStream把信息存入内存中的一个缓沖区中

) FileOutputStream把信息存入文件中

) PipedOutputStream实现了pipe的概念主要在线程中使用

) SequenceOutputStream把多个OutStream合并为一个OutStream

以Unicode字符为导向的stream

以Unicode字符为导向的stream表示以Unicode字符为单位从stream中读取或往stream中写入信息以Unicode字符为导向的stream包括下面几种类型

) InputStream

) CharArrayReader与ByteArrayInputStream对应

) StringReader与StringBufferInputStream对应

) FileReader与FileInputStream对应

) PipedReader与PipedInputStream对应

) OutStream

) CharArrayWrite与ByteArrayOutputStream对应

) StringWrite无与之对应的以字节为导向的stream

) FileWrite与FileOutputStream对应

) PipedWrite与PipedOutputStream对应

以字符为导向的stream基本上对有与之相对应的以字节为导向的stream两个对应类实现的功能相同字是在操作时的导向不同如CharArrayReader和ByteArrayInputStream的作用都是把内存中的一个缓沖区作为InputStream使用所不同的是前者每次从内存中读取一个字节的信息而后者每次从内存中读取一个字符

两种不现导向的stream之间的转换

InputStreamReader和OutputStreamReader把一个以字节为导向的stream转换成一个以字符为导向的stream

stream添加属性

为stream添加属性的作用

运用上面介绍的Java中操作IO的API我们就可完成我们想完成的任何操作了但通过FilterInputStream和FilterOutStream的子类我们可以为stream添加属性下面以一个例子来说明这种功能的作用

如果我们要往一个文件中写入数据我们可以这样操作

FileOutStream fs = new FileOutStream(testtxt);

然后就可以通过产生的fs对象调用write()函数来往testtxt文件中写入数据了但是如果我们想实现先把要写入文件的数据先缓存到内存中再把缓存中的数据写入文件中的功能时上面的API就没有一个能满足我们的需求了但是通过FilterInputStream和FilterOutStream的子类为FileOutStream添加我们所需要的功能

FilterInputStream的各种类型

用于封装以字节为导向的InputStream

) DataInputStream从stream中读取基本类型(intchar等)数据

) BufferedInputStream使用缓沖区

) LineNumberInputStream会记录input stream内的行数然后可以调用getLineNumber()和setLineNumber(int)

) PushbackInputStream很少用到一般用于编译器开发

用于封装以字符为导向的InputStream

) 没有与DataInputStream对应的类除非在要使用readLine()时改用BufferedReader否则使用DataInputStream

) BufferedReader与BufferedInputStream对应

) LineNumberReader与LineNumberInputStream对应

) PushBackReader与PushbackInputStream对应

FilterOutStream的各种类型

用于封装以字节为导向的OutputStream

) DataIOutStream往stream中输出基本类型(intchar等)数据

) BufferedOutStream使用缓沖区

) PrintStream产生格式化输出

用于封装以字符为导向的OutputStream

) BufferedWrite与对应

) PrintWrite与对应

RandomAccessFile

) 可通过RandomAccessFile对象完成对文件的读写操作

) 在产生一个对象时可指明要打开的文件的性质r只读w只写rw可读写

) 可以直接跳到文件中指定的位置

I/O应用的一个例子

import javaio*;

public class TestIO{

public static void main(String[] args)

throws IOException{

//以行为单位从一个文件读取数据

BufferedReader in =

new BufferedReader(

new FileReader(F:\\nepalon\\TestIOjava));

String s s = new String();

while((s = inreadLine()) != null)

s += s + \n;

inclose();

//b 接收键盘的输入

BufferedReader stdin =

new BufferedReader(

new InputStreamReader(Systemin));

Systemoutprintln(Enter a line:);

Systemoutprintln(stdinreadLine());

// 从一个String对象中读取数据

StringReader in = new StringReader(s);

int c;

while((c = inread()) != )

Systemoutprintln((char)c);

inclose();

// 从内存取出格式化输入

try{

DataInputStream in =

new DataInputStream(

new ByteArrayInputStream(sgetBytes()));

while(true)

Systemoutprintln((char)inreadByte());

}

catch(EOFException e){

Systemoutprintln(End of stream);

}

// 输出到文件

try{

BufferedReader in =

new BufferedReader(

new StringReader(s));

PrintWriter out =

new PrintWriter(

new BufferedWriter(

new FileWriter(F:\\nepalon\\ TestIOout)));

int lineCount = ;

while((s = inreadLine()) != null)

outprintln(lineCount++ + + s);

outclose();

inclose();

}

catch(EOFException ex){

Systemoutprintln(End of stream);

}

// 数据的存储和恢复

try{

DataOutputStream out =

new DataOutputStream(

new BufferedOutputStream(

new FileOutputStream(F:\\nepalon\\ Datatxt)));

outwriteDouble();

outwriteChars(\nThas was pi:writeChars\n);

outwriteBytes(Thas was pi:writeByte\n);

outclose();

DataInputStream in =

new DataInputStream(

new BufferedInputStream(

new FileInputStream(F:\\nepalon\\ Datatxt)));

BufferedReader inbr =

new BufferedReader(

new InputStreamReader(in));

Systemoutprintln(inreadDouble());

Systemoutprintln(inbrreadLine());

Systemoutprintln(inbrreadLine());

}

catch(EOFException e){

Systemoutprintln(End of stream);

}

// 通过RandomAccessFile操作文件

RandomAccessFile rf =

new RandomAccessFile(F:\\nepalon\\ rtestdat rw);

for(int i=; i <; i++)

rfwriteDouble(i*);

rfclose();

rf = new RandomAccessFile(F:\\nepalon\\ rtestdat r);

for(int i=; i <; i++)

Systemoutprintln(Value + i + + rfreadDouble());

rfclose();

rf = new RandomAccessFile(F:\\nepalon\\ rtestdat rw);

rfseek(*);

rfwriteDouble();

rfclose();

rf = new RandomAccessFile(F:\\nepalon\\ rtestdat r);

for(int i=; i <; i++)

Systemoutprintln(Value + i + + rfreadDouble());

rfclose();

}

}

关于代码的解释(以区为单位)

区中当读取文件时先把文件内容读到缓存中当调用inreadLine()时再从缓存中以字符的方式读取数据(以下简称缓存字节读取方式

b区中由于想以缓存字节读取方式从标准IO(键盘)中读取数据所以要先把标准IO(Systemin)转换成字符导向的stream再进行BufferedReader封装

区中要以字符的形式从一个String对象中读取数据所以要产生一个StringReader类型的stream

               

上一篇:如何有效防止Java程序源码被人偷窥?

下一篇:关于Java Socket编程的详细介绍