IO流(第九天):字节输入流,字节输出流,字符输入流,字符输出流;

一,输入,输出的流向

     

 二.File类

import java.io.File;
import java.io.IOException;

public class Filr {
    public static void main(String[] args) throws IOException {
        //构造方法
        //File file = new File("D:\\demo\\a.txt");//根据一个路径得到File对象
        //File file1 = new File("D:\\demo","a.txt");//根据一个目录和一个子文件/目录得到File对象
        File file2 = new File("a");//这个路径可以是文件夹的路径,还可以是不存在的路径
        //File file3 = new File(file2,"a.txt");//根据一个父File对象和一个子文件/目录得到File对象
        //三种方式效果一样
        //成员方法
          //创建功能
      File f = new File("c.txt");//创建文件 如果存在这样的文件,就不创建了
        boolean newFile = f.createNewFile();
        boolean mkdir = file2.mkdir();//创建文件夹,如果存在这样的文件夹,就不创建了
        //删除功能
        //boolean delete = f.delete();
        //注:要删除一个文件夹,请注意该文件夹内不能包含文件或者文件夹
        //file2.delete();
        //重命名功能
        File f2 = new File("b.txt");
        f.renameTo(f2);//如果路径名相同,就是改名。
        //判断功能
        boolean directory = f.isDirectory();//判断是否是目录
        System.out.println(directory);
        boolean file = f.isFile();//判断是否是文件
        /*public boolean exists():判断是否存在
        public boolean canRead():判断是否可读
        public boolean canWrite():判断是否可写
        public boolean isHidden():判断是否隐藏
        */
        //基本路径:
        String absolutePath = f.getAbsolutePath();//获取绝对路径
        String path = f.getPath();//获取相对路径
        String name = f.getName();//获取名称
        long length = f.length();//获取长度
        String[] list = f.list();//返回该路径下所有的文件/文件夹名称
        File[] files = f.listFiles();//返回返回该文件夹下的所有的文件对象
    }
}


三,IO流的的分类

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

        ②根据方向:字节流 字符流

        字节输入流:InputStream--抽象父类-不能实例化

                           FileInputStream--文件字节输入流,构造函数的参数:File、String

                           BuffereInputStream--高效字节输入流

import java.io.*;

public class Demo {
    public static void main(String[] args) throws IOException {
        //method1();//普通字节流读取
        method2();//高效字节流读取
    }

    private static void method2() throws IOException {
        InputStream in=null;
        try {
           //new BufferedInputStream(new FileInputStream("b.txt"));
           in= new BufferedInputStream(new FileInputStream(new File("b.txt")));
            int b;
            while((b=in.read())!=-1){
                System.out.println(b);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            in.close();
        }

    }
private static void method1() throws IOException {
            InputStream in=null;
    try {
         in = new FileInputStream(
                new File("b.txt"));
        //InputStream in = new FileInputStream("a.txt");
        System.out.println(in.read());
        System.out.println(in.read());
        System.out.println(in.read());
        /*while(true){
            if (in.read()!=-1){
                System.out.println(in.read());
            }
        }*///不可以,这种写法会跳着读,一部分数据用来做判断条件。没有打印,会丢失数据
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        in.close();
    }
}
}

        字节输出流:OutputStream--抽象父类,不能实例化    

                            FileOutputStream--文件字节输出流

                            BufferedOutputStream--高校字节输出流

import java.io.*;

public class Demo3 {
    public static void main(String[] args) throws IOException {
        //method1();
        method2();

    }

    private static void method2() throws IOException {
        OutputStream out =null;
        try {
            out =new BufferedOutputStream(new FileOutputStream(new File("b.txt")));
                out.write(1);
                out.write(2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            out.close();
        }

    }

    private static void method1() {
        OutputStream out = null;
        try {
            out=new FileOutputStream(new File("b.txt"));
        out.write(97);
            out.write(98);
            out.write(99);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

        字符输入流:Reader --抽象类--不能实例化

                           FileReader--文件字节输入流

                            BufferedRrader--高校字节输入流

import java.io.*;

public class Demo2 {
    public static void main(String[] args) throws IOException {
       // method1();
        method2();
    }

    private static void method2() throws IOException {
        Reader in= null;
        try {
            in=new BufferedReader(new FileReader(new File("b.txt")));
            int b;
            while((b=in.read())!=-1){
                System.out.println(b);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            in.close();
        }

    }

    private static void method1() throws IOException {
        Reader in=null;
        //创建对象
        try {
            //Reader in = new FileReader("b.txt");
            in = new FileReader(new File("b.txt"));
            int b;
        //读取内容
            while((b=in.read())!=-1){
                System.out.println(b);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
         //关流
            in.close();
        }
    }
}

        字符输出流:Writer

                字符输出流:Writer--抽象弗列不能实例化

                FileWriter

                BufferedWriter

import java.io.*;

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

    private static void method2() {
        BufferedWriter out=null;
        try {
            out = new BufferedWriter(new FileWriter(new File("b.txt")),true);//(false)覆盖和(true)追加
            out.write(2);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void method1() {
        Writer out =null;
        try {
            out =new FileWriter(new File("b.txt"));
            out.write(5);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员慕慕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值