CGB2107 2021.8.16 day12 笔记

IO流

1.流Stream

单方向,程序读入输入in,输出out,从头到尾读写一次

2.File文件类

首先,创建File类的对象 File file=new File("pathname.....");

1)构造函数的参数类型是String,代表的是要操作的路径pathname , 这个路径既可以是文件路径也可以是文件夹路径,还可以是不存在的路径

2)\在代码中有特殊的意义,表示转义符号,所以想要表示路径中的\, 要写两个才能表示单纯的\

文件与文件夹属性:

①file.length();获取指定文件的字节量

②file.exists()判断文件是否存在

③file.isFile()判断是否为文件

④file.isDirectory()判断是否为文件夹

⑤file.getName() 获取文件名

⑥file.getParent()获取上级路径

⑦file.getAbsolutePath()获取全路径

//创建File文件类对象,注意需要导包:java.io
        File file =new File("D:\\ready\\1.txt");
        //测试文件与文件夹属性
        System.out.println(file.length());//获取指定文件的字节量
        System.out.println(file.exists());//判断文件是否存在
        System.out.println(file.isFile());//判断是否为文件
        System.out.println(file.isDirectory());//判断是否为文件夹
        System.out.println(file.getName());//获取文件名
        System.out.println(file.getParent());//获取上级路径
        System.out.println(file.getAbsolutePath());//获取全路径

创建与删除:

File file =new File("D:\\ready\\1.txt");

file.createNewFile()  创建不存在的文件

注意:如果指定创建文件的路径不存在,会报错:java.io.IOException ,系统找不到指定的路径就会发生异常,所以调用时需要抛出异常

file.mkdir(); 创建不存在的单层文件夹

file.mkdirs() 创建不存在的多层文件夹

file.delete() 删除文件或者空文件夹

//2.2测试创建与删除
        file=new File("D:\\ready\\2.txt");
        /*如果指定创建文件的路径不存在,会报错:java.io.IOException
        * 系统找不到指定的路径就会发生异常,所以调用时需要抛出异常*/
        System.out.println(file.createNewFile());
        file =new File("D:\\ready\\m");
        System.out.println(file.mkdir());
        file=new File("D:\\ready\\a\\b\\c");
        System.out.println(file.mkdirs());
        /*delete()只能用来删除文件或者空文件夹*/
        System.out.println(file.delete());//c被删除
        file =new File("D:\\ready\\a");
        System.out.println(file.delete());//a里有文件夹不能删
        file =new File("D:\\ready\\2.txt");
        System.out.println(file.delete());

文件列表测试:

file.list(); 

file.listFiles();

 //2.3文件列表测试
        file =new File("D:\\ready");
        String[] a = file.list();//Alt+Enter引入局部变量接近返回值
        System.out.println(Arrays.toString(a));
//        System.out.println(a[0].isFile());会报错,String不能用File的API
        File[] fs = file.listFiles();
        System.out.println(Arrays.toString(fs));
        System.out.println(fs[0].length());

3.流的分类

根据方向:输入流、输出流

根据操作单位:字节流、字符流

1)字节输入流:InputStream(抽象父类)

子类FileInputStream、BufferedInputStream

read()每次调用都会读取一个数据字节,如果读到了数据的末尾则返回-1

package cn.tedu.file;

import java.io.*;

//本类用于测试字节输入流
public class TestIn {
    public static void main(String[] args) {
//        method1();//普通字节流读取
        method2();//高效字节流读取
    }
    //使用高效字节流读取
    private static void method2() {
        InputStream in=null;
        //1.创建流对象
        try {
//            InputStream in=new BufferedInputStream(new FileInputStream(new File("D:\\ready\\1.txt")));
            in=new BufferedInputStream(new FileInputStream("D:\\ready\\1.txt"));
            //2.使用流对象
            int b;
            while((b=in.read())!=-1){
                System.out.println(b);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //3.关闭字节流
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        }

    //使用普通字节流读取
    private static void method1() {
        InputStream in=null;
        //1.创建字节输入流对象来读取
        try {
//            InputStream in=new FileInputStream(new File("D:\\ready\\1.txt"));
            in=new FileInputStream("D:\\ready\\1.txt");
            //2.使用字节输入流对象进行读取
            /*read()每次调用都会读取一个数据字节,如果读到了数据的末尾则返回-1*/
//            System.out.println(in.read());
//            System.out.println(in.read());
//            System.out.println(in.read());
//            System.out.println(in.read());//-1
            //需求:循环读取文件中的所有内容直至结束
            int b;
            //不可以,这种写法会跳着读,一部分数据用来做判断条件了,没有打印,会丢失数据
//            while (in.read()!=-1){
//                System.out.println(in.read());
//            }

            while((b=in.read())!=-1){
                System.out.println(b);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            /*try-catch结构中的第三个部分:finally()\
            * 无论是否捕获到异常,是一定会被执行到的代码块
            * 所以常常用来关流操作*/
            //3.关闭流资源
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

try-catch结构中的第三个部分:finally{}无论是否捕获到异常,是一定会被执行到的代码块 * 所以常常用来关流操作

2)字节输出流:OutputStream(抽象父类)

                             FileOutputStream

                             BufferdeOutputStream

package cn.tedu.file;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

//本类用于练习字节输出流
public class TestOut {
    public static void main(String[] args) {
//        method1();//普通字节输出流
        method2();//高效字节输出流
    }
    private static void method2() {
        OutputStream out=null;
        try{
            out=new BufferedOutputStream(new FileOutputStream("D:\\ready\\1.txt",true));
            out.write(98);
            out.write(98);
            out.write(98);
            out.write(98);

        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void method1() {
        OutputStream out=null;
        try{
//            out=new FileOutputStream("D:\\ready\\1.txt");
            out=new FileOutputStream("D:\\ready\\1.txt",true);
            /*FOS的构造函数有一个参数append(),默认值为false,表示覆盖
            * 如果设置为true,那么每次输出内容会加在原来的文件内容之后,不覆盖*/
            out.write(97);
            out.write(97);
            out.write(97);
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

3)字符输入流:Reader(抽象父类)

子类FileReader、BufferedReader

package cn.tedu.file;

import java.io.*;

//本类用于练习字符输入流
public class TestIn2 {
    public static void main(String[] args) {
//        method1();//用于普通字符流的读取
        method2();//用于高效字符流的读取
    }

    private static void method2() {
        Reader in=null;
        try{
            in=new BufferedReader(new FileReader(new File("D:\\ready\\1.txt")));
            in=new BufferedReader(new FileReader("D:\\ready\\1.txt"));
            int b;
            while((b=in.read())!=-1){
                System.out.println(b);
            }


        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    private static void method1() {
        Reader in=null;
        //1.创建流对象
        try {
//            Reader in=new FileReader(new File("D:\\ready\\1.txt"));
            in=new FileReader("D:\\ready\\1.txt");
            //2.使用流对象
            int b;
            while((b=in.read())!=-1){
                System.out.println(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //3.关流
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }




    }
}


4)字符输出流 Writer

                         FileWriter

                        BufferedWriter

package cn.tedu.file;
//本类用于测试字符输出流
import java.io.*;

public class TestOut2 {
    public static void main(String[] args) {
        method1();
        method2();
    }

    private static void method2() {
        Writer out=null;
        try{
//            out=new BufferedWriter(new FileWriter(new File("D:\\ready\\1.txt"),true));
            out=new BufferedWriter(new FileWriter("D:\\ready\\1.txt",true));
            out.write(97);
            out.write(97);
            out.write(97);


        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private static void method1() {
        Writer out=null;
        try{
            out=new FileWriter("D:\\ready\\1.txt",true);
            out.write(99);
            out.write(99);
            out.write(99);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值