IO学习笔记

IO

File

概述

image-20200826212300032

构造方法

image-20200826212415299

代码实现:

public class FileDemo001  {

    public static void main(String[] args) {
        File f1 = new File("D:\\file\\file.txt");
        System.out.println(f1);

        File f2 = new File("D:\\file","file.txt");
        System.out.println(f2);

        File f3 = new File("D:\\file");
        File f4 = new File(f3,"file.txt");
        System.out.println(f4);

    }
}

结果:

D:\file\file.txt
D:\file\file.txt
D:\file\file.txt

File类创建功能

image-20200826213225337

代码实现:

public class FileDemo002 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:\\file\\java.txt");
        System.out.println(f1.createNewFile());

        File f2 = new File("D:\\file\\javac");
        System.out.println(f2.mkdir());

        File f3 = new File("D:\\file\\javase\\io");
        System.out.println(f3.mkdirs());
        
         File f4 = new File("D:\\file\\javac.txt");
        //要根据调用的方法来判断创建的是什么类型文件
        //System.out.println(f4.mkdir());//java.txt文件夹
        System.out.println(f4.createNewFile());//java.txt
    }
}

结果:

true
true
true
true

File类判断与获取功能

image-20200826214822080

代码实现:

public class FileDemo003 {
    public static void main(String[] args) {
        File f1 = new File("D:\\file\\java.txt");

        System.out.println(f1.isDirectory());
        System.out.println(f1.exists());
        System.out.println(f1.isFile());

        System.out.println(f1.getAbsolutePath());
        System.out.println(f1.getName());
        System.out.println(f1.getPath());
        System.out.println("========分隔符======");

        File f2 = new File("D:\\file");
        String[] list = f2.list();
        for (String s : list) {
            System.out.println(s);
        }
        
        System.out.println("========分隔符======");

        File[] files = f2.listFiles();
        for (File f : files) {
            System.out.println(f);

        }
    }
}

结果:

false
true
true
D:\file\java.txt
java.txt
D:\file\java.txt
========分隔符======
file.txt
java.txt
javac
javac.txt
javase
========分隔符======
D:\file\file.txt
D:\file\java.txt
D:\file\javac
D:\file\javac.txt
D:\file\javase

IO流

概述

image-20200826220106118

image-20200826220205226

字节流写数据

image-20200827102610066

使用字节输出流写数据的步骤

image-20200827102721296

代码实现:

public class FileOutputStreamDemo001 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("基础语法\\fos.txt");

        fos.write(97);
        
        //关闭资源
        fos.close();
    }
}

这里涉及到了资源关闭问题,附上链接

https://www.jianshu.com/p/00806fa2cd40

字节流写数据的几种方法·

image-20200827104333358

代码实现:

public class FileOutputStreamDemo002 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream(new File("基础语法\\fos.txt"));

        //第一种
        fos.write(55);
        fos.write(56);
        fos.write(57);
        fos.write(58);

        //第二种
        byte[] bys = {55,56,57,58};
        fos.write(bys);

        //第三种
        byte[] b = "abcde".getBytes();
        fos.write(b,0,5);
    }
}

拓展两个小问题

image-20200827111216517

image-20200827111243894

字节流读数据

一次读一个字节

image-20200828074553296

代码实现:

public class FileIntputStreamDemo001 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("基础语法\\fos.txt");;
        int by;
        while((by=fis.read())!=-1){
            System.out.print((char)by);
        }
        fis.close();
    }
}

结果:

abcde
Process finished with exit code 0

案例复制文本文件

image-20200828093602265

image-20200828093602265

代码实现:

image-20200828093942635

一次读一个字节数组数据

代码实现:

public class FileIntputStreamDemo002 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("基础语法\\fos.txt");

        byte[] bys = new byte[1024];
        int len = 0;
        while((len = fis.read(bys))!=-1){
            System.out.println(new String(bys,0,len));
        }

        fis.close();
    }

}

结果:

abcde
efghf

案例:复制图片

image-20200828095919819

image-20200828100044163

字节缓冲流

image-20200828103426759

image-20200828103348200

案例:复制视频

image-20200828103723794

代码实现:

https://www.bilibili.com/video/BV1Ei4y137HJ?p=288

字符流

为什么出现字符流

image-20200828141447847

字符流写数据的几种方式

image-20200828201418684

public class OutputStreamWriterDemo001 {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("基础语法\\osw.txt"));

        osw.write(97);
        osw.flush();//刷新

        char[] ch = {'a','b','c','d','e'};
        osw.write(ch);

        osw.write(ch,0,4);
        
        osw.write("abcde");
        
        osw.write("abcde",0,"abcde".length());
        
        osw.close();

    }
}

字符流读数据

image-20200828202830805

代码实现:

public class OutputStreamWriterDemo002 {
    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream("基础语法\\isr.txt"));

        int ch;
        while ((ch = isr.read())!= -1){
            System.out.print((char)ch);
        }

        byte[] bys = new byte[1024];
        int len;
        while ((len = isr.read())!= -1){
            System.out.print(new String(bys,0,len));
        }
    }
}

案例:复制java文件

image-20200828203703657

image-20200828203915309

代码改进:

image-20200828204053290

image-20200828204139190

字符缓冲流

image-20200828205911330

代码实现:

image-20200828210210390

例:复制java文件*

image-20200828210330959

image-20200828210439330

特有功能

image-20200828210529284

案例:复制java文件

image-20200828211048636

posted @ 2021-02-04 16:24  初出茅庐小菜鸡  阅读(68)  评论(0编辑  收藏  举报