Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26187775
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2010-02-25 22:57:42

JAVAIO类整理(初级)

Java有两种流:InputOutput

全部的流可以分为两种类型:

1、  以字节为导向的stream

含义:表示以字节为单位从stream中读取或往stream中写入信息!

按输入与输出来划分

1.1  input stream

可以分为如下的几个类

1) ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使用
2) StringBufferInputStream
:把一个String对象作为InputStream
3) FileInputStream
:把一个文件作为InputStream,实现对文件的读取操作
4) PipedInputStream
:实现了pipe的概念,主要在线程中使用
5) SequenceInputStream
:把多个InputStream合并为一个InputStream

 

1.1    Out  stream

可以分为如下的几个类型:

1) ByteArrayOutputStream:把信息存入内存中的一个缓冲区中
2) FileOutputStream
:把信息存入文件中
3) PipedOutputStream
:实现了pipe的概念,主要在线程中使用
4) SequenceOutputStream
:把多个OutStream合并为一个OutStream

 

总结:以字节为导向的流可以分为九种。包容了全部的输入与输出流类!

PSinput stream 表示的是 东西从文件传入到内存区域

 

右箭头: 要

内存区域

 

文件区域

 
 

 

 


我们说的input output 的话都是站在内存区域的角度来说明问题的。

Input表示 内存区域进来数据 即从文件区域读东西。

PS:读文件的思路

1、  建立与文件的链接。得到一个文件句柄 示例File files = new File("a");

得到了一个句柄之后(相当于建立数据库连接得到了一个连接数据库对象的句柄)

2、创建读取的对象。用文件句柄来创建哦。相当于数据库连接的时候一样的需要建立起来连接对象方才能用连接对象去操作文件了!

inputStream = new FileInputStream(files);

到此,把管道搭过去了就可以对此文件进行读取操作了!

 

2、  Unicode字符为导向的stream

含义:以Unicode字符为导向的stream,表示以Unicode字符为单位从stream中读取或往stream中写入信息。

PS:注意好两者之间的区别即一个是以字节为单位进行传输而现在这个则是以Unicode字符为单位进行传输

一样按照输入与输出流进行划分可以如下

2.1 Input  Stream
1) CharArrayReader
:与ByteArrayInputStream对应
2) StringReader
:与StringBufferInputStream对应
3) FileReader
:与FileInputStream对应
4) PipedReader
:与PipedInputStream对应


2.2  Out Stream
1) CharArrayWrite
:与ByteArrayOutputStream对应
2) StringWrite
:无与之对应的以字节为导向的stream
3) FileWrite
:与FileOutputStream对应
4) PipedWrite
:与PipedOutputStream对应

 

注意:字符为单位和字节为单位的差异

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

(传输的单位不一样而已所造成的)

 

3、  两种不同的流之间进行相互转换

InputStreamReaderOutputStreamReader:把一个以字节为导向的stream转换成一个以字符为导向的stream

 

从字符流到字节流:可以从字符流中获取char[]数组,转换为String,然后调用StringAPI函数getBytes() 获取到byte[],然后就可以通过ByteArrayInputStreamByteArrayOutputStream来实现到字节流的转换。

 

PS:通过这两个类就可以实现 两种不同的单位传输之间的转换输出

注意两者之间的差异

字节流和字符流,字节流读取的最小单位是一个字节(1byte=8bit),而字符流一次可以读取一个字符(1char = 2byte = 16bit)故而字符为单位的读写一次单位会比字节的要大!

 

4、  按流的功能划分

分节点流和处理流

节点流是直接从一个源读写数据的流(这个流没有经过包装和修饰),处理流是在对节点流封装的基础上的一种流,FileInputStream是一个接点 流,可以直接从文件读取数据,但是BufferedInputStream可以包装FileInputStream,使得其有缓冲功能

 

PS:经常会看到一些IO读取数据的时候,做了一下包装处理。即实现了所谓的处理流功能

这种处理输出的主要一个作用就是可以有缓冲功能

 

第二部分:实例

2.1        读文件
1
、按字节读取文件内容
2
、按字符读取文件内容
3
、按行读取文件内容
4
、随机读取文件内容

示例代码一:按字节一个字节一个字节读取指定文件内容

//以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。

public class IOUtils {

    public static void main(String[] args) {

       File files = new File("D://a.jpg");

       InputStream inputStream = null;

       try {

           inputStream = new FileInputStream(files);

           int tempbyte;

           try {

              //表示一次只读取一个字节 一直读到字未尾结束

              while ((tempbyte = inputStream.read()) != -1) {

                  System.out.println(tempbyte);

              }

              inputStream.close();

           } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

           }

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

}

示例代码一:按字节一次读取多个字节读取指定文件内容

public class IOUtils {

    public static void main(String[] args) {

       File files = new File("D://a.jpg");

       InputStream inputStream = null;

       try {

           inputStream = new FileInputStream(files);

           byte[] tempbytes = new byte[100];

           int byteread = 0;

           try {

              System.out.println("total bytes is :" + String.valueOf(inputStream.available()));

              while ((byteread = inputStream.read(tempbytes)) != -1) {

                  System.out.write(tempbytes,0,byteread);

              }

           } catch (IOException e1) {

              // TODO Auto-generated catch block

              e1.printStackTrace();

           } finally {

              if (inputStream != null) {

                  try {

                     inputStream.close();

                  } catch (IOException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

                  }

              }

           }         

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

}

可以在读取数据的时候指定大小。一定指取指定的数据大小即可!

 

示例三:按字符读取

以字符为单位读取文件,常用于读文本,数字等类型的文件

PS:我明白了上面为什么要有按字节与字符两种类型的读取方式了。主要是针对图片与文字这两种类型的对象的时候能够分开处理好

public class IOUtils {

    public static void main(String[] args) {

       File files = new File("D://a.txt");

       Reader reader = null;

       try {

           reader = new InputStreamReader(new FileInputStream(files));

//PS:里面的InputStreamReader的作用就是实现将字节流转换成字符流输出!

           int tempchar;

           while ((tempchar = reader.read()) != -1) {

              if (((char)tempchar) != 'r') {

                  System.out.println((char)tempchar);

              }

           }

           reader.close();

       } catch (Exception e) {

           // TODO: handle exception

       }

      

    }

}

 

PS:字符=2字节    所以字节是可以很容易转换成字符。即先读取字节数据然后再读取字符

 

示例四:通过字符读取并且指定每次读取的块大小

public class IOUtils {

    public static void main(String[] args) {

       File files = new File("D://a.txt");

       Reader reader = null;

       char[] tempchars = new char[30];//每次读30个字节

       try {

           reader = new InputStreamReader(new FileInputStream(files));

           int tempchar;

           while ((tempchar = reader.read(tempchars)) != -1) {

              if ((tempchar == tempchars.length) && (tempchars[tempchars.length - 1] != 'r')) {

                  System.out.println(tempchars);

              }else{

                  for(int i=0;i

                     if (tempchars[i] == 'r') {

                         continue;

                     }else {

                         System.out.println(tempchars[i]);

                     }

                  }

              }

           }

           reader.close();

       } catch (Exception e) {

           // TODO: handle exception

       } finally{

           if (reader != null) {

              try {

                  reader.close();

              } catch (IOException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

              }

           }

       }

      

    }

}

 

 

 

示例五:按行读取

public class IOUtils {

    public static void main(String[] args) {

       File files = new File("D://a.txt");

       BufferedReader reader = null;

       int line = 1;

       String tempString = null;

       try {

           reader = new BufferedReader(new FileReader(files));

对缓冲读数据的操作!需要加上一个文件句柄过来才行的!

           while ((tempString = reader.readLine()) != null) {

              System.out.println("line "+ line + " :" + tempString);

              line++;

           }

           reader.close();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } finally {

           if (reader != null) {

              try {

                  reader.close();

              } catch (IOException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

              }

           }

       }

      

      

    }

}


第二版:新推出JAVA版的IO操作工具类库!


 

 

阅读(1158) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~