黑马程序员 <a href="http://www.itheima.com" target="blank">java培训</a>

            第20天笔记

1.IO概述及分类

         IO:  input  Output

         IO流:输入输出流

计算机底层,是二进制,计算机之间进行信息交换,传递的也是二进制。

通过IO流我们的计算机才可以进行信息交换。

2.关于java中IO流的分类

         讲三种分法:

  1. 按照流的流向来划分

a)        输入流     XxxxInputStream   XxxReader

b)        输出流     XxxxoOutputStream   XxxxWriter

  1. 按照传输单位

流最底层操作的全是二进制。

a)        字节流----直接操作二进制   XxxxInputStream   XxxOutputStream

b)        字符流----操作的是字符   XxxReader  XxxWriter

  1. 按照使用方式

a)        基本流  简单理解:可以直接对源或目的地操作(例如文件)

b)        复合流  它不能直接对源或目的地操作,它需要在包装一个基本流。

 

在java.io包下有四个类

           InputStream

OutputStream

Reader

Wieter

以上四个类都是abstract类,它们是所有的IO流的基类(根类,父类)

 

public class FileInputStreamDemo1 {

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

        // 1.创建FileInputStream
        FileInputStream fis = new FileInputStream("a.txt");

        // 2.调用read()方法读取一个字节
        // int code1 = fis.read();
        //
        // System.out.println(code1);
        //
        // int code2 = fis.read();
        //
        // System.out.println(code2);
        //
        // int code3 = fis.read();
        //
        // System.out.println(code3);
        //
        //
        // int code4 = fis.read();
        //
        // System.out.println(code4);

//        while (true) {
//            int code = fis.read();
//            if (code == -1) { //读到-1代表结束
//                break;
//            }
//            System.out.println((char)code);
//        }
        
        //在开发中代码可能会是以下写法
        int code=-1;
        while((code=fis.read())!=-1){            
            System.out.println((char)code);
        }

        // 3.关闭
        fis.close();

    }
}

 

3.IO基类概述和一个简单的需求分析

                  InputStream  字节输入流的超类

OutputStream 字节输出流的超类

Reader 字符输入

Wieter  字符输出

注意一个规范:字节流都是以InputStream  OutputStream结尾,字符流都是以 Reader Writer结尾     

一个文件要将其内容读取出来:输入流 InputStream   Reader

对于输入流来说:它一定会提供读取操作的方法    read方法

向一个文件中写入内容:输出流   OutputStream Writer

对于输出流来说:它一定会提供写操作的方法.   Write方法

public class FileInputStreamDemo2 {

    public static void main(String[] args) throws IOException {
        // 将a.txt文件内容读取出来

        // 1.创建流
        FileInputStream fis = new FileInputStream("a.txt");

        // 2.读取
        byte[] b = new byte[3];

        // int len=fis.read(b); //len代表的是装入到数组中的有效字节个数

        // int len1=fis.read(b);
        // System.out.println(Arrays.toString(b));
        // int len2=fis.read(b);
        // System.out.println(Arrays.toString(b));
        //
        // int len3=fis.read(b);
        // System.out.println(len3);
        // System.out.println(Arrays.toString(b));

        while (true) {
            int len = fis.read(b); //一次读取b.length个字节信息,返回值是装入了数组中有效的字节个数
            if (len == -1) { //如果为-1代表读到文件末尾
                break;
            }
            // 怎样将byte[]数组转换成字符串
            String s = new String(b, 0, len); //将数组中从0开始的装入的len个有效的字节合成字符串。
            System.out.print(s);
        }

        // 3.关闭
        fis.close();
    }
}

 

4.关于字符和字节的选择:

                   第一种分法:

                       如果操作的是文本文件:一般情况下使用字符流.

                       如果操作的是二进制文件:一般情况下使用字节流.

              第二种分法:

                       只想做一个文件的复制,就是使用字节流.(文件下载)

 

 

 

 

4.    FileOutputStream的构造方法

 new FileOutputStream(String name)

new FileOutputStream(File file);

new FileOutputStream(String name,boolean append);

new FileOutputStream(File file,boolean append);

关于FileOutputStream类的构造方法注意事项:

A.使用它的构造方法会产生FileNotFoundException,它是一个编译异常,需要处理.

B.如果使用这个构造,操作的文件不存在,路径没问题的前提下,会创建文件。

C.在使用这个构造时,如果指定了boolean参数,并且为true,代表是在文件后追加,而不会覆盖。也就是说,如果为false,或不写,它会执行覆盖操作。

 

public class FileOutputStreamDemo1 {

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

    // 演示 new FileOutputStreasm(String name);
    public static void demo1() throws IOException {

        FileOutputStream fos = new FileOutputStream("a.txt"); // 注意产生异常,抛出.
                                                                // FileNotFoundException
        // 我使用的是a.txt这个相对路径,它的位置应该是day20工程下.
        // 如果我们指定的目的地,也就是那个文件,如果不存在,会自动创建.
        // 如果我们指定的目的地,也就是那个文件,存在,会覆盖。

        fos.write('h'); // public void write(int b)
        fos.write('e');
        fos.write('l');
        fos.write('l');
        fos.write('o');

        // 问题:要向a.txt文件写出内容? a.txt文件在哪? a.txt文件是否存在?

    }

    // 演示 new FileOutputStreasm(File file);
    public static void demo2() throws IOException {

        File file = new File("a.txt");
        FileOutputStream fos = new FileOutputStream(file); // 注意产生异常,抛出.

        fos.write('h');
        fos.write('e');
        fos.write('l');
        fos.write('l');
        fos.write('o');

    }

    // 演示 new FileOutputStreasm(File file,boolean append);
    public static void demo3() throws IOException {

        File file = new File("a.txt");
        FileOutputStream fos = new FileOutputStream(file, true);
        // 第二个参数是boolean类型的,如果为false,与不写一样,都会覆盖,如果为true,它会在文件的末尾进行追加操作,而不会覆盖.
        fos.write('h');
        fos.write('e');
        fos.write('l');
        fos.write('l');
        fos.write('o');

    }

 

5.FileOutputStream写出数据

 

 

流操作的步骤:

  1. 创建流  FileOutputStream fos=new FileOutputStream(xxx);
  2. 调用write方法操作
  3. 调用close方法闭
    public class FileOutputStreamDemo2 {
    
        public static void main(String[] args) throws IOException {
            // 1.创建一个FileOutputStream
    
            FileOutputStream fos = new FileOutputStream("a.txt"); //抛出异常   FileNotFoundException
    
            // 2.调用其write方法写信息
            String s = "hello world";    
    
            byte[] b = s.getBytes(); // 转换成字节数组
    
            for (int i = 0; i < b.length; i++) {
                fos.write(b[i]); // 将数组中每一个字节信息写到文件中.  //产生异常  IOException
            }
    
            // 3.关闭资源
            fos.close();
        }
    }

     

FileOutputStream的三个write 方法

介绍一下关于FileOutputStream中三个重载的write方法

 

public void write(int code) 一次写入一个字节,code值代表的是写入的字节信息码值,它只能写入int数据的后八位,前24位舍弃。

public void write(byte[] b) 一次写入b.length个字节

public void write(byte[] b ,int start,int len)  将b数组中从start开始的len个写入.

 

public class FileOutputStreamDemo3 {

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

    // 演示 write(byte[] b)
    public static void demo1() throws IOException {
        FileOutputStream fos = new FileOutputStream("a.txt"); // 抛出异常
                                                                // FileNotFoundException

        // 2.调用其write方法写信息
        String s = "hello world";

        byte[] b = s.getBytes(); // 转换成字节数组

        fos.write(b); // 一次将b.length信息全部写入
        // 3.关闭资源
        fos.close();
    }

    // 演示 write(byte[] b,int start,int len)
    public static void demo2() throws IOException {
        FileOutputStream fos = new FileOutputStream("a.txt"); // 抛出异常
                                                                // FileNotFoundException

        // 2.调用其write方法写信息
        String s = "hello world";

        byte[] b = s.getBytes(); // 转换成字节数组

        fos.write(b, 6, 5); // 一次将b数组中从下标6开始的5个字节信息写入.
        // 3.关闭资源
        fos.close();
    }
}

 

6.FileOutputStream写出数据实现换行和追加写入

  从键盘信息,如果输入的不是exit,那么就将这个信息写入到一个指定文件中,并且,每一次输入的信息,都是单独一行.

描述:程序运行,请输入信息:hello world  就会将hello world写入到一个文件中,并且是单独一行。

程序继续提示:请输入信息,如果输入的不是exit,就将信息一直写入,并且都是单独一行,直到输入exit结束.

7.FileOutpuStream写出数据加入异常处理

关于流操作时的异常处理:

FileOutputStream fos=null;  //声明

try{ //处理异常

  write()

         read()

}catch(IOException e){

}

finally{

//关闭

           try{

           if(xxx!=null){

                    Close()

}

}catch(IOException){

 

}

}

 

8.FileInputStream读取数据

FileInputStream它的作用?

它可以从文件中按字节读取信息.

 

FileInputStream的使用与FileOutputStream用法差不多.

 

  1. 构造方法

new FileInputStream(File f)

new FileInputStream(String name)

参数就是要操作的文件,也就是源.

  1. 读操作

int read()  一次读取一个字节,返回的就是读到的字节码值,如果返回值为-1代表读取文件末尾

 

int read(byte[] b) 一次可以读取b.length个字节,返回值代表的是装入到b数组的有效字节个数.,如果返回值为-1代表读取到文件末尾

 

int read(byte[] b ,int start,int len) 一次读取从start开始的len这些字节信息

 

 

public class FileOutputStreamDemo5 {

    public static void main(String[] args) {
        // 1.创建输出流
        FileOutputStream fos = null;
        // 2.定义一个Scanner
        Scanner sc = new Scanner(System.in);

        try {
            fos = new FileOutputStream("src/msg.txt", true); // 就可以追加写入
            // 3.从键盘接收
            while (true) {
                System.out.println("请输入信息");
                String line = sc.nextLine();

                if ("exit".equalsIgnoreCase(line)) { // 不区分大小写
                    break;
                }
                // 4.将信息写入到文件中.
                byte[] b = (line + "\r\n").getBytes(); // 在信息后加上\r\n在得到byte[]。
                fos.write(b);

            }
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {

            // 5.关闭
            try {
                if (fos != null) {
                    fos.close(); // 如果fos没有创建成功.会产生NullPointerException,所有我们一般情况下要判断后在操作.
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

posted on 2015-04-16 00:29  星之钥匙  阅读(128)  评论(0编辑  收藏  举报