IO流

来源:互联网 发布:蜂窝网络 编辑:程序博客网 时间:2024/06/17 02:33

Java的输出流基类 :OutputStream和Writer类

Java的输入流基类:InputStream和Reader类


按照处理数据单元划分:

    01.字节流(操作的最小数据单元为8位的字节):

           001.字节输入流:InputStream基类

           002.字节输出流:OutputStream基类

   02.字符流(操作的最小数据单元为16位的字符):

           001.字符输入流:Reader基类

           002.字符输出流:Writer基类


使用字节读取文本文件:

  01.我们通常使用InputStream的子类FileInputStream来实现文本文件内容的读取

       读取文本文件内容常用的构造方法:

          001.FileInputStream(File file):

                 file指定数据源

                 File file = new File("C:\\myDoc:\\hello.txt");

                 InputStream fileObject = new FileInputStream(file);

          002.FileInputStream(String name):

                 name指定文件数据源,包含路径信息

                 InputStream in = new FileInputStream("C:\\hello.txt");

       InputStream中读取数据的常用方法

                 方法名称                                               说明

                 int read();                                  读取一个字节数据

                 int read(byte [ ]b);                      将数据读取字节数组中  

                 int read(byte[ ]b,int off,int len);  从数据流中读取到最多len长度的字节,保存到字节数组b中是,保存的

                                                                    位置从off开始

                 void close();                               关闭输入流

                 int available();                            返回输入流读取的估计字节数

     具体步骤

                001.引入相关类

                       import java.io.*;     

                002.创建一个文件输入流对象

                       InputStream in = new FileInputStream("C:\\hello.txt");

                003.利用文件输入流的方法读取文本文件内容

                       in.read();

                       ........

                004.关闭文件输入流对象

                      in.close();

示例:

        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:\\myDoc\\hello.txt");
            int data;
            System.out.println("可读取的字节数是:"+fis.available());
            System.out.println("文件内容为:");
            //循环读数据
            while((data=fis.read())!=-1){
                System.out.print((char)data+" ");
            }

        } catch (FileNotFoundException e) {
            
            e.printStackTrace();
        } catch (IOException e) {
        
            e.printStackTrace();
        }finally{
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


使用字节流写文本文件

        通常使用OutputStream的子类FileOutputStream来实现向文本文件写入数据

        写入数据到文本文件内容常用的构造方法:

          001.FileOutputStream(File file):

                 file指定数据源

                 File file = new File("C:\\myDoc:\\hello.txt");

                 FileOutputStream fileObject = new FileOutputStream(file);

          002.FileOutputStream(String name):

                 name指定文件数据源,包含路径信息

                 FileOutputStream in = newFileOutputStream("C:\\hello.txt");

         003.FileOutputStream(String name,boolean append) :

                name指定文件数据源,包含路径信息.append表示是否在文件末尾添加数据,若设置为true,则在文件

               末尾添加数据。

               FileOutputStream in = newFileOutputStream("C:\\hello.txt",true);

       第一种和第二种构造方法在向文件写数据时会覆盖文件中的内容


OutputStream中读取数据的常用方法

                 方法名称                                               说明

                 void write(int c);                              写入一字节数据

                 void write(byte[ ] buf);                     写入数组buf的所有字节

                 void write(byte [ ] buf,int off,int len);  将字节数组从off位置开始,长度为len的字节数据输出到输出流中

                 void close();                                     关闭输出流


具体步骤

              01.引入相关类:

                  import java.io.*;  

              02.构建一个文件输出流对象:

                  OutputStream fos = new FileOutputStream("C:\\hello.txt");

              03.利用文件输出流的方法把数据写入文本文件中

                   String str = "好好学习java";

                   byte [ ] words = new str.getByte();

                   //利用write方法写入文件中

                   fos.write(words,0,words.length);

              04.关闭文件输出流

                  fos.close();

示例:

    FileOutputStream fos = null;
    String str = "study Java";
    byte[] words = str.getBytes();   //字节数组
    try {
        //创建流对象,以追加方式写入文件
        fos = new FileOutputStream("D:\\myDoc\\tian.txt");
        //写入文件
        fos.write(words, 0, words.length);
        System.out.println("\ntian文件已更新!");
    } catch (IOException e) {
        System.out.println("创建文件时出错!");
    }finally{
        if(fos!=null){
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


未完待续。。。。。
























 

                   

































原创粉丝点击