服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

彻底明白java的io系统

   一. input和output

  1. stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。在java的io中,所有的stream(包括input和out stream)都包括两种类型:



 1.1 以字节为导向的stream

  以字节为导向的stream,表示以字节为单位从stream中读取或往stream中写入信息。以字节为导向的stream  包括下面几种类型:
  1) input stream:
  1) bytearrayinputstream:把内存中的一个缓冲区作为inputstream使用
  2) stringbufferinputstream:把一个string对象作为inputstream
  3) fileinputstream:把一个文件作为inputstream,实现对文件的读取操作
  4) pipedinputstream:实现了pipe的概念,主要在线程中使用
  5) sequenceinputstream:把多个inputstream合并为一个inputstream
  2) out stream
  1) bytearrayoutputstream:把信息存入内存中的一个缓冲区中
  2) fileoutputstream:把信息存入文件中
  3) pipedoutputstream:实现了pipe的概念,主要在线程中使用
  4) sequenceoutputstream:把多个outstream合并为一个outstream

  1.2 以unicode字符为导向的stream

  以unicode字符为导向的stream,表示以unicode字符为单位从stream中读取或往stream中写入信息。以   unicode字符为导向的stream包括下面几种类型:
 1) input stream
  1) chararrayreader:与bytearrayinputstream对应
  2) stringreader:与stringbufferinputstream对应
  3) filereader:与fileinputstream对应
  4) pipedreader:与pipedinputstream对应
  2) out stream
  1) chararraywrite:与bytearrayoutputstream对应
  2) stringwrite:无与之对应的以字节为导向的stream
  3) filewrite:与fileoutputstream对应
  4) pipedwrite:与pipedoutputstream对应

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

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

  inputstreamreader和outputstreamreader:把一个以字节为导向的stream转换成一个以字符为导向的stream。

  2. stream添加属性

  2.1 “为stream添加属性”的作用

  运用上面介绍的java中操作io的api,我们就可完成我们想完成的任何操作了。但通过filterinputstream和filteroutstream的子类,我们可以为stream添加属性。下面以一个例子来说明这种功能的作用。
如果我们要往一个文件中写入数据,我们可以这样操作:


fileoutstream fs = new fileoutstream(“test.txt”);



  然后就可以通过产生的fs对象调用write()函数来往test.txt文件中写入数据了。但是,如果我们想实现“先把要写入文件的数据先缓存到内存中,再把缓存中的数据写入文件中”的功能时,上面的api就没有一个能满足我们的需求了。但是通过filterinputstream和filteroutstream的子类,为fileoutstream添加我们所需要的功能。
  2.2 filterinputstream的各种类型






  2.2.1 用于封装以字节为导向的inputstream
 
  1) datainputstream:从stream中读取基本类型(int、char等)数据。
  2) bufferedinputstream:使用缓冲区
  3) linenumberinputstream:会记录input stream内的行数,然后可以调用getlinenumber()和setlinenumber(int)
  4) pushbackinputstream:很少用到,一般用于编译器开发

  2.2.2 用于封装以字符为导向的inputstream

  1) 没有与datainputstream对应的类。除非在要使用readline()时改用bufferedreader,否则使用datainputstream
  2) bufferedreader:与bufferedinputstream对应
  3) linenumberreader:与linenumberinputstream对应
  4) pushbackreader:与pushbackinputstream对应

  2.3 filteroutstream的各种类型

  2.2.3 用于封装以字节为导向的outputstream

  1) dataioutstream:往stream中输出基本类型(int、char等)数据。
  2) bufferedoutstream:使用缓冲区
  3) printstream:产生格式化输出
  2.2.4 用于封装以字符为导向的outputstream

  1) bufferedwrite:与对应
  2) printwrite:与对应
  3. randomaccessfile
  1) 可通过randomaccessfile对象完成对文件的读写操作
  2) 在产生一个对象时,可指明要打开的文件的性质:r,只读;w,只写;rw可读写
  3) 可以直接跳到文件中指定的位置

  4. i/o应用的一个例子

import java.io.*;
public class testio{
public static void main(string[] args)
throws ioexception{
//1.以行为单位从一个文件读取数据
bufferedreader in =
new bufferedreader(
new filereader("f://nepalon//testio.java"));
string s, s2 = new string();
while((s = in.readline()) != null)
s2 += s + "/n";
in.close();

//1b. 接收键盘的输入
bufferedreader stdin =
new bufferedreader(
new inputstreamreader(system.in));
system.out.println("enter a line:");
system.out.println(stdin.readline());

//2. 从一个string对象中读取数据
stringreader in2 = new stringreader(s2);
int c;
while((c = in2.read()) != -1)
system.out.println((char)c);
in2.close();










//3. 从内存取出格式化输入
try{
datainputstream in3 =
new datainputstream(
new bytearrayinputstream(s2.getbytes()));
while(true)
system.out.println((char)in3.readbyte());
}
catch(eofexception e){
system.out.println("end of stream");
}

//4. 输出到文件
try{
bufferedreader in4 =
new bufferedreader(
new stringreader(s2));
printwriter out1 =
new printwriter(
new bufferedwriter(
new filewriter("f://nepalon// testio.out")));
int linecount = 1;
while((s = in4.readline()) != null)
out1.println(linecount++ + ":" + s);
out1.close();
in4.close();
}
catch(eofexception ex){
system.out.println("end of stream");
}

//5. 数据的存储和恢复
try{
dataoutputstream out2 =
new dataoutputstream(
new bufferedoutputstream(
new fileoutputstream("f://nepalon// data.txt")));
out2.writedouble(3.1415926);
out2.writechars("/nthas was pi:writechars/n");
out2.writebytes("thas was pi:writebyte/n");
out2.close();
datainputstream in5 =
new datainputstream(
new bufferedinputstream(
new fileinputstream("f://nepalon// data.txt")));
bufferedreader in5br =
new bufferedreader(
new inputstreamreader(in5));
system.out.println(in5.readdouble());
system.out.println(in5br.readline());
system.out.println(in5br.readline());
}










catch(eofexception e){
system.out.println("end of stream");
}

//6. 通过randomaccessfile操作文件
randomaccessfile rf =
new randomaccessfile("f://nepalon// rtest.dat", "rw");
for(int i=0; i<10; i++)
rf.writedouble(i*1.414);
rf.close();

rf = new randomaccessfile("f://nepalon// rtest.dat", "r");
for(int i=0; i<10; i++)
system.out.println("value " + i + ":" + rf.readdouble());
rf.close();

rf = new randomaccessfile("f://nepalon// rtest.dat", "rw");
rf.seek(5*8);
rf.writedouble(47.0001);
rf.close();

rf = new randomaccessfile("f://nepalon// rtest.dat", "r");
for(int i=0; i<10; i++)
system.out.println("value " + i + ":" + rf.readdouble());
rf.close();
}
}



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

1区中,当读取文件时,先把文件内容读到缓存中,当调用in.readline()时,再从缓存中以字符的方式读取数据(以下简称“缓存字节读取方式”)。

1b区中,由于想以缓存字节读取方式从标准io(键盘)中读取数据,所以要先把标准io(system.in)转换成字符导向的stream,再进行bufferedreader封装。

2区中,要以字符的形式从一个string对象中读取数据,所以要产生一个stringreader类型的stream。

4区中,对string对象s2读取数据时,先把对象中的数据存入缓存中,再从缓冲中进行读取;对testio.out文件进行操作时,先把格式化后的信息输出到缓存中,再把缓存中的信息输出到文件中。

5区中,对data.txt文件进行输出时,是先把基本类型的数据输出屋缓存中,再把缓存中的数据输出到文件中;对文件进行读取操作时,先把文件中的数据读取到缓存中,再从缓存中以基本类型的形式进行读取。注意in5.readdouble()这一行。因为写入第一个writedouble(),所以为了正确显示。也要以基本类型的形式进行读取。

6区是通过randomaccessfile类对文件进行操作。






扫描关注微信公众号