韩顺平 Java IO流 自学笔记

目录

一.文件基础知识

1.文件基础知识铺垫

2.创建文件的三种方式

3.获取文件信息

 4.目录操作

二.IO流原理及流的分类

1.IO流的原理

 2.流的分类

三.InputStream字节输入流

1.InputStream的简介

 2.FileInputStream类方法

四.OutputSteam字节输出流

1.OutputSteam类的简介

2..FileOutPutStream类

五.writer字符输出流

1.writer简介

2. FileWriter类

六.reader字符输入流

1.reader类的简介

 2.FileReader类

七.节点流和处理流 

1.相关概念

2.处理流的设计模式

3.处理流BufferReader和BufferWriter

4.对象处理流ObjectOutputStream和ObjectInputStream 序列化与反序列化

5.标准输入与输出流System.in 和System.out

 6.转换流InputStreamReader和OutputStreamWriter

7.打印流PrintStream和PrintWriter

七.配置文件Properties

1.引出配置文件 

2.基本的使用​编辑

八.家庭作业

1.家庭作业一

2.家庭作业二

3.家庭作业三


一.文件基础知识

前情提要:Io最主要的是区分不同流的作用和使用规范

1.文件基础知识铺垫

1.1什么是文件

文件就是保存数据的地方,图片,音乐,视频等等。 

1.2文件流的概念 

 

 输入与输出是针对内存的,磁盘内容输入到java程序当中,Java程序处理的结果输出到磁盘当中。

类比:

IO流的整个类图

2.创建文件的三种方式

案例 

 代码

package com.file;

import org.junit.jupiter.api.Test;

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

public class FileCrate {
    public static void main(String[] args) {

    }
    @Test
    public void create01(){
//        指定文件的路径
        String filePath = "d:\\one.txt";
//      还没有进行创建,相当于对文件进行声明
        File file = new File(filePath);
//        进行创建
        try {
            file.createNewFile();
            System.out.println("文件创建成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
//     方法二,通过父文件和子路径来进行创建  参数为File,String  File有三种创建对象的构造器
    public void create02(){
        File parentFile = new File("d:\\");

        String path = "next.txt";
//      在java程序(内存)当中创建的对象,使用方法,将内存当中的对象,输出到硬盘上
        File file = new File(parentFile, path);
//      创建文件
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//    public File(String parent, String child) 第三种方法
    @Test
    public void create03(){
        String parent = "d:\\";

        String path = "next01.txt";
//      在java程序(内存)当中创建的对象,使用方法,将内存当中的对象,输出到硬盘上
        File file = new File(parent, path);
//      创建文件
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行结果

 指定路径下就会出现我们想要的文件了

3.获取文件信息

常用的方法

 案例演示

代码

package com.file;

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

public class FileInformation {
    public static void main(String[] args) {
//        先创建文件
        File file = new File("d:\\no1.txt");
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        调用相应的方法得到相应的信息
        System.out.println("文件的名字  "+file.getName());
        System.out.println("文件的父级目录  "+file.getParent());
        System.out.println("文件的绝对路径  "+file.getPath());
        System.out.println("文件的大小(字节)  "+file.length());
        System.out.println("文件是否存在  "+file.exists());
        System.out.println("是不是一个文件  "+file.isFile());
        System.out.println("是不是一个目录  "+file.isDirectory());
    }
}

 运行结果

文件的名字  no1.txt
文件的父级目录  d:\
文件的绝对路径  d:\no1.txt
文件的大小(字节)  6

 4.目录操作

用到的方法

 代码

package com.file;

import org.junit.jupiter.api.Test;

import java.io.File;

public class Directory_ {
    public static void main(String[] args) {

    }
//    判断m1是否存在,存在就删除
    @Test
    public void m1(){
        String filePath = "d:\\no1.txt";

        File file = new File(filePath);

        if (file.exists()){
            if (file.delete()){
                System.out.println(filePath+"文件删除成功");
            }else {
                System.out.println(filePath+"文件删除失败");
            }
        }else {
            System.out.println("该文件不存在");
        }
    }

//    判断demo01.txt是否存在,不存在打印不存在  在java当中,目录也被当成一种文件
@Test
public void m2(){
    String filePath = "d:\\demo01";

    File file = new File(filePath);

    if (file.exists()){
        if (file.delete()){
            System.out.println(filePath+"文件删除成功");
        }else {
            System.out.println(filePath+"文件删除失败");
        }
    }else {
        System.out.println("该文件不存在");
    }
}
//  判断文件夹有没有,没有就创建文件夹
@Test
public void m3(){
    String filePath = "d:\\demo01";

    File file = new File(filePath);

    if (file.exists()){
            System.out.println(filePath+"文件存在");
    }else {
//        创建一个目录
        if (file.mkdirs()){
            System.out.println(filePath+"文件夹创建成功。");
        }else{
            System.out.println(filePath+"文件夹创建失败。");
        }
    }
}
}

m3运行结果

  

二.IO流原理及流的分类

1.IO流的原理

 2.流的分类

三.InputStream字节输入流

1.InputStream的简介

类图结构

 2.FileInputStream类方法

代码

package com.inputstream;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStream_ {
    public static void main(String[] args) {

    }
//    单个读取的效率太低,使用public int read(byte b[])方法。
        @Test
    public void readFile01() throws IOException {
        String filePath = "d:\\hello.txt";
        int read =0 ;
//        创建输入流
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream(filePath);
//            对数据进行循环的读取,当返回值为-1时,表示读取完毕

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

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
//            关闭流,进行资源的释放
            fileInputStream.close();
        }
    }
    @Test
    public void readFile02() throws IOException {
        String filePath = "d:\\hello.txt";
        int readLen =0 ;
//        创建字节数组,一次读取八个字节
        byte[] bytes = new byte[8];
//        创建输入流
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream(filePath);

//          方法进行优化
            while ((readLen = fileInputStream.read(bytes))!=-1){
//            讲字节转化为字符串进行输出
                System.out.print(new String(bytes,0,readLen));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
//            关闭流,进行资源的释放
            fileInputStream.close();
        }
    }
}

Test2对读取的方式进行了优化

四.OutputSteam字节输出流

1.OutputSteam类的简介

类图

2..FileOutPutStream类

常用的方法

应用实例1

在代码指定的位置创建文件

 代码

package com.outputstream;

import org.junit.jupiter.api.Test;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStream01 {
    public static void main(String[] args) {

    }

    /*
    * 使用FileOutputStream将文件写入内存当中*/
    @Test
    public void writeFile()  {
        String filePath = "d:\\a.txt";
//        创建文件输出流对象
        FileOutputStream fileOutputStream = null;

        try {
//            new FileOutputStream(filePath)写入内容时会覆盖原来的内容
//            new FileOutputStream(filePath,true)进行追加而不是覆盖
            fileOutputStream = new FileOutputStream(filePath,true);
//            write方法的三种使用
            //        1.输出数据  单个字符数据
            fileOutputStream.write('H');
//            2.写入字符串数据
            String str = "hello.word!";
//            将String变成byte数组的形式在传入   使用string自带的方法
            fileOutputStream.write(str.getBytes());
//            3.写入一段字符串  public void write(byte b[], int off, int len)  off为开始截取的位置,len为截取的长度
            fileOutputStream.write(str.getBytes(),0,3);

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

}

 运行结果:指定文件当中的文本内容被修改

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值