java中IO流

File类
方法名说明
File(String pathname)通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例
File(String parent, String child)从父路径名字符串和子路径名字符串创建新的 File实例
File(File parent, String child)从父抽象路径名和子路径名字符串创建新的 File实例
package cn.zzj;

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

public class Test {
    public static void main(String[] args) throws IOException {
        //File(String pathname): 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例
        File f1 = new File("E:\\test\\a.txt");
        System.out.println(f1);
        //File(String parent, String child): 从父路径名字符串和子路径名字符串创建新的 File实例
        File f2 = new File("E:\\test","a.txt");
        System.out.println(f2);
        //File(File parent, String child): 从父抽象路径名和子路径名字符串创建新的 File实例
        File f3 = new File("E:\\test");
        File f4 = new File(f3,"a.txt");
        System.out.println(f4);

        // 是一个完整的路径,从盘符开始
        File file1 = new File("E:\\test\\a.txt");
        // 是一个简化的路径,从当前项目根目录开始
        File file2 = new File("b.txt");
        // 从当前模块下创建文件
        File file3 = new File("模块名\\c.txt");
        System.out.println(file3.createNewFile());
    }

}

方法名说明
public boolean createNewFile()当具有该名称的文件不存在时,创建一个由该抽象路径名命名的新空文件,文件路径必须存在,否则找不到指定路径报错
public boolean mkdir()创建由此抽象路径名命名的目录
public boolean mkdirs()创建由此抽象路径名命名的目录,包括任何必需但不存在的父目录
package cn.zzj;

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



public class Test {
    public static void main(String[] args) throws IOException {
        //需求1:我要在E:\\test目录下创建一个文件java.txt
        File f1 = new File("E:\\test\\java.txt");
        System.out.println(f1.createNewFile());
        System.out.println("--------");

        //需求2:我要在E:\\test目录下创建一个目录JavaSE
        File f2 = new File("E:\\test\\JavaSE");
        System.out.println(f2.mkdir());
        System.out.println("--------");

        //需求3:我要在E:\\test目录下创建一个多级目录JavaWEB\\HTML
        File f3 = new File("E:\\test\\JavaWEB\\HTML");
//        System.out.println(f3.mkdir());
        System.out.println(f3.mkdirs());
        System.out.println("--------");
        
    }

}



方法名说明
public boolean delete()删除由此抽象路径名表示的文件或目录(只能删除空文件夹)
package cn.zzj;

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



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

        File f1 = new File("E:\\test\\java.txt");
        System.out.println(f1.createNewFile());
        System.out.println("--------");
        //删除文件
        System.out.println(f1.delete());
        System.out.println("--------");

        //需求2:我要在E:\\test目录下创建一个目录JavaSE
        File f2 = new File("E:\\test\\JavaSE");
        System.out.println(f2.mkdir());
        System.out.println("--------");
        //删除空文件
        System.out.println(f2.delete());
        System.out.println("--------");

        File f3 = new File("E:\\test\\Java");
        System.out.println(f3.mkdir());
        File f4 = new File("E:\\test\\Java\\a.txt");
        System.out.println(f4.createNewFile());
        //删除不是空的文件夹 删除失败
        System.out.println(f3.delete());
        System.out.println("--------");

    }

}

判断功能

方法名说明
public boolean isDirectory()测试此抽象路径名表示的File是否为目录
public boolean isFile()测试此抽象路径名表示的File是否为文件
public boolean exists()测试此抽象路径名表示的File是否存在

获取功能

方法名说明
public String getAbsolutePath()返回此抽象路径名的绝对路径名字符串
public String getPath()将此抽象路径名转换为路径名字符串
public String getName()返回由此抽象路径名表示的文件或目录的名称
public File[] listFiles()返回此抽象路径名表示的目录中的文件和目录的File对象数组
package cn.zzj;

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



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

        //创建一个File对象
        File f = new File("HotKey\\a.txt");
        System.out.println(f.isDirectory());
        System.out.println(f.isFile());
        System.out.println(f.exists());

        System.out.println(f.getAbsolutePath());
        System.out.println(f.getPath());
        System.out.println(f.getName());
        System.out.println("--------");

        File f2 = new File("E:\\test");
        File[] fileArray = f2.listFiles();
        for(File file : fileArray) {
            System.out.println(file);
            System.out.println(file.getName());
        }

    }

}

遍历删除文件夹中的所有文件

public class Test {
    public static void main(String[] args) throws IOException {
        File src = new File("E:\\test");
        deleteDir(src);
    }

    //1.定义一个方法,接收一个File对象
    private static void deleteDir(File src) {
        //1.先删掉这个文件夹里面所有的内容.
        //2.遍历这个File对象,获取它下边的每个文件和文件夹对象
        File[] files = src.listFiles();
        //3.判断当前遍历到的File对象是文件还是文件夹
        for (File file : files) {
            //4.如果是文件,直接删除
            if(file.isFile()){
                file.delete();
            }else{
                //5.如果是文件夹,递归调用自己,将当前遍历到的File对象当做参数传递
                deleteDir(file);//参数一定要是src文件夹里面的文件夹File对象
            }
        }
        //6.参数传递过来的文件夹File对象已经处理完成,最后直接删除这个空文件夹
        src.delete();
    }
}

字节流

字节输出流

  • 字节流抽象基类
    • InputStream:这个抽象类是表示字节输入流的所有类的超类
    • OutputStream:这个抽象类是表示字节输出流的所有类的超类
    • 子类名特点:子类名称都是以其父类名作为子类名的后缀
  • 字节输出流
    • FileOutputStream(String name):创建文件输出流以指定的名称写入文件
方法名
void write(int b)将指定的字节写入此文件输出流 一次写一个字节数据
void write(byte[] b)将 b.length字节从指定的字节数组写入此文件输出流 一次写一个字节数组数据
void write(byte[] b, int off, int len)将 len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流 一次写一个字节数组的部分数据
public class Test {
    public static void main(String[] args) throws IOException {
//        1.如果文件不存在,会帮我们创建
//        2.如果文件存在,会把文件清空
        //加入finally来实现释放资源
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("HotKey\\a.txt");
//            FileOutputStream(File file):创建文件输出流以写入由指定的 File对象表示的文件
//            FileOutputStream fos = new FileOutputStream(new File("HotKey\\a.txt"));
 //           fos.write(97);
            byte[] bys = "hello".getBytes();
//            fos.write(bys);
            fos.write(bys, 1, 3);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        //创建字节输出流对象 第二个参数为true 则从文件末尾添加写数据,如果文件有数据则不会清空
        FileOutputStream fos2 = new FileOutputStream("HotKey\\b.txt", true);
        //写数据
        for (int i = 0; i < 10; i++) {
            fos2.write("hello".getBytes());
            fos2.write("\r\n".getBytes());
        }
        //释放资源
        fos.close();
        fos2.close();
    }


}

字节输入流

  • FileInputStream(String name):通过打开与实际文件的连接来创建一个FileInputStream,该文件由文件系统中的路径名name命名
  • 一次读一个字节数组的方法
    • public int read(byte[] b):从输入流读取最多b.length个字节的数据
    • 返回的是读入缓冲区的总字节数,也就是实际的读取字节个数
  • 示例代码
public class Test {
    public static void main(String[] args) throws IOException {
        //根据数据源创建字节输入流对象
        FileInputStream fis = new FileInputStream("HotKey\\ba1.jpg");
        //根据目的地创建字节输出流对象
        FileOutputStream fos = new FileOutputStream("HotKey\\ba2.jpg");

        //读写数据,复制文本文件(一次读取一个字节,一次写入一个字节)
//        int by;
//        while ((by=fis.read())!=-1) {
//            fos.write(by);
//        }
        //读写数据,复制图片(一次读取一个字节数组,一次写入一个字节数组)
        byte[] bys = new byte[1024];
        int len;
        while ((len=fis.read(bys))!=-1) {
            fos.write(bys,0,len);
        }
        //释放资源
        fos.close();
        fis.close();
    
    }


}


字节缓冲流

字节缓冲流介绍(主要功能是提高读写效率)

  • lBufferOutputStream:该类实现缓冲输出流.通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致底层系统的调用
  • lBufferedInputStream:创建BufferedInputStream将创建一个内部缓冲区数组.当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次很多字节
方法名说明
BufferedOutputStream(OutputStream out)创建字节缓冲输出流对象
BufferedInputStream(InputStream in)创建字节缓冲输入流对象
public class Test {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("HotKey\\ba1.jpg"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("HotKey\\ba1copy.jpg"));
        //一个字节一个字节读
        //        int by;
        //        while ((by=bis.read())!=-1) {
        //            bos.write(by);
        //        }

        //一组字节读
        byte[] bys = new byte[1024];
        int len;
        while ((len = bis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }

        bos.close();
        bis.close();

    }


}

字符流
  • 字符流的介绍
    由于字节流操作中文不是特别的方便,所以Java就提供字符流
    字符流 = 字节流 + 编码表
  • 中文的字节存储方式
    用字节流复制文本文件时,文本文件也会有中文,但是没有问题,原因是最终底层操作会自动进行字节拼接成中文,如何识别是中文的呢?
    汉字在存储的时候,无论选择哪种编码存储,第一个字节都是负数

相关方法

方法名说明
byte[] getBytes()使用平台的默认字符集将该 String编码为一系列字节
byte[] getBytes(String charsetName)使用指定的字符集将该 String编码为一系列字节
String(byte[] bytes)使用平台的默认字符集解码指定的字节数组来创建字符串
String(byte[] bytes, String charsetName)通过指定的字符集解码指定的字节数组来创建字符串
public class Test {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //定义一个字符串
        String s = "筱田优";
        //默认编码和UTF-8字节一样说明默认编码是以UTF-8编码
        byte[] bys = s.getBytes(); //[-25, -83, -79, -25, -108, -80, -28, -68, -104]
        byte[] bys2 = s.getBytes("UTF-8"); //[-25, -83, -79, -25, -108, -80, -28, -68, -104]
        byte[] bys3 = s.getBytes("GBK"); //[-13, -29, -52, -17, -45, -59]
        System.out.println(Arrays.toString(bys));
        System.out.println(Arrays.toString(bys2));
        System.out.println(Arrays.toString(bys3));

        String ss = new String(bys);
        String ss2 = new String(bys2,"UTF-8");
        String ss3 = new String(bys3,"GBK");
        System.out.println(ss);
        System.out.println(ss2);
        System.out.println(ss3);

    }
}

字符流写数据

  • 介绍
    Writer: 用于写入字符流的抽象父类
    FileWriter: 用于写入字符流的常用子类

  • 构造方法

方法名说明
FileWriter(File file)根据给定的 File 对象构造一个 FileWriter 对象
FileWriter(File file, boolean append)根据给定的 File 对象以及指示是否附加写入数据的 boolean 值构造一个 FileWriter 对象
FileWriter(String fileName)根据给定的文件名构造一个 FileWriter 对象
FileWriter(String fileName, boolean append)根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象
  • 成员方法
方法名说明
void write(int c)写一个字符
void write(char[] cbuf)写入一个字符数组
void write(char[] cbuf, int off, int len)写入字符数组的一部分
void write(String str)写一个字符串
void write(String str, int off, int len)写一个字符串的一部分
  • 刷新和关闭的方法
方法名说明
flush()刷新流,之后还可以继续写数据
close()关闭流,释放资源,但是在关闭之前会先刷新流。一旦关闭,就不能再写数据
public class Test {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("HotKey\\a.txt");

//        void write(int c):写一个字符
//        fw.write(97);
//
//        void writ(char[] cbuf):写入一个字符数组
//        char[] chs = {'a', 'b', 'c', 'd', 'e'};
//        fw.write(chs);
//
//        void write(char[] cbuf, int off, int len):写入字符数组的一部分
//        fw.write(chs, 0, chs.length);
//        fw.write(chs, 1, 3);
//
//        void write(String str):写一个字符串
//        fw.write("abcde");
//
//        void write(String str, int off, int len):写一个字符串的一部分
//        fw.write("abcde", 0, "abcde".length());
        fw.write("abcde", 1, 3);

        //释放资源
        fw.close();

    }


}

字符流读数据

  • 介绍
    Reader: 用于读取字符流的抽象父类
    FileReader: 用于读取字符流的常用子类
  • 构造方法
方法名说明
FileReader(File file)在给定从中读取数据的 File 的情况下创建一个新 FileReader
FileReader(String fileName)在给定从中读取数据的文件名的情况下创建一个新 FileReader
  • 成员方法
方法名说明
int read()一次读一个字符数据
int read(char[] cbuf)一次读一个字符数组数据
public class Test {
    public static void main(String[] args) throws IOException {
    	//你的文件是什么编码就用什么编码读
        Charset charset = Charset.forName("GBK");
        FileReader fr = new FileReader("HotKey\\a.txt",charset);

        //int read():一次读一个字符数据
//        int ch;
//        while ((ch=fr.read())!=-1) {
//            System.out.print((char)ch);
//        }

        //int read(char[] cbuf):一次读一个字符数组数据
        char[] chs = new char[1024];
        int len;
        while ((len = fr.read(chs)) != -1) {
            String s = new String(chs, 0, len);
            System.out.println(s);
        }
        //释放资源
        fr.close();
    }

}

字符缓冲流(也是为了提高读写文件的效率)
字符缓冲流介绍

  • BufferedWriter:将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入,可以指定缓冲区大小,或者可以接受默认大小。默认值足够大,可用于大多数用途
  • BufferedReader:从字符输入流读取文本,缓冲字符,以提供字符,数组和行的高效读取,可以指定缓冲区大小,或者可以使用默认大小。 默认值足够大,可用于大多数用途

构造方法

方法名说明
BufferedWriter(Writer out)创建字符缓冲输出流对象
BufferedReader(Reader in)创建字符缓冲输入流对象
public class Test {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("HotKey\\a.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("HotKey\\b.txt"));

        //一次读取一个字符数组数据
        char[] chs = new char[1024];
        int len;
        while ((len=br.read(chs))!=-1) {
            bw.write(chs, 0,len);
        }

        br.close();
        bw.close();
    }


}

字符缓冲流特有功能

  • BufferedWriter
    void newLine():写一行行分隔符,行分隔符字符串由系统属性定义
  • BufferedReader
    String readLine():读一行文字。 结果包含行的内容的字符串,不包括任何行终止字符如果流的结尾已经到达,则为null
public class Test {
    public static void main(String[] args) throws IOException {
        //创建字符缓冲输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("HotKey\\a.txt"));
        //写数据
        for (int i = 0; i < 10; i++) {
            bw.write("hello" + i);
            //bw.write("\r\n");
            bw.newLine();
            bw.flush();
        }
        //释放资源
        bw.close();

        //创建字符缓冲输入流
        BufferedReader br = new BufferedReader(new FileReader("HotKey\\a.txt"));
        String line;
        while ((line=br.readLine())!=null) {
            System.out.println(line);
        }
        br.close();
    }


}
转换流
  • InputStreamReader:是从字节流到字符流的桥梁,父类是Reader
    它读取字节,并使用指定的编码将其解码为字符
    它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集
  • OutputStreamWriter:是从字符流到字节流的桥梁,父类是Writer
    是从字符流到字节流的桥梁,使用指定的编码将写入的字符编码为字节
    它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集
方法名说明
InputStreamReader(InputStream in)使用默认字符编码创建InputStreamReader对象
InputStreamReader(InputStream in,String chatset)使用指定的字符编码创建InputStreamReader对象
OutputStreamWriter(OutputStream out)使用默认字符编码创建OutputStreamWriter对象
OutputStreamWriter(OutputStream out,String charset)使用指定的字符编码创建OutputStreamWriter对象
public class Test {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("HotKey\\w.txt"),"GBK");
        osw.write("中国");
        osw.close();

        InputStreamReader isr = new InputStreamReader(new FileInputStream("HotKey\\w.txt"),"GBK");
        //一次读取一个字符数据
        int ch;
        while ((ch=isr.read())!=-1) {
            System.out.println((char)ch);
        }
        isr.close();
    }
}
对象序列化流

序列化

  • 构造方法
    • ObjectOutputStream(OutputStream out)
      创建一个写入指定的OutputStream的ObjectOutputStream
  • 序列化对象的方法
    • void writeObject(Object obj)
      将指定的对象写入ObjectOutputStream

反序列化

  • 构造方法

    • ObjectInputStream(InputStream in)
      创建从指定的InputStream读取的ObjectInputStream
  • 反序列化对象的方法

    • Object readObject()
      从ObjectInputStream读取一个对象
package cn.zzj;

import java.io.Serializable;

public class Student implements Serializable {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
package cn.zzj;

import java.io.*;


public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //序列化
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("HotKey\\a.txt"));
        Student s1 = new Student("筱田优",30);
        oos.writeObject(s1);
        oos.close();

        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("HotKey\\a.txt"));
        Object obj = ois.readObject();
        Student s2 = (Student) obj;
        System.out.println(s2.getName() + "," + s2.getAge());
        ois.close();
    }


}



序列化问题

  • serialVersionUID

    • 用对象序列化流序列化了一个对象后,假如我们修改了对象所属的类文件 会出问题,会抛出InvalidClassException异常,因为如果我们没有定义序列号,java虚拟机会根据类的信息定义一个序列号,如果我们修改后会重新生成一个序列号,这就造成我修改类后生成的序列号和上一次写进去的序列号不一致
    • 解决方案
      • 重新序列化
      • 给对象所属的类加一个serialVersionUID
        • private static final long serialVersionUID = 42L;
  • transient

    • 如果一个对象中的某个成员变量的值不想被序列化,又该如何实现呢?
      • 给该成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程
package cn.zzj;

import java.io.Serializable;

public class Student implements Serializable {
    private String name;
//    private int age;
    //这个成员变量不参与序列化
    private transient int age;
    //自定义序列号
    private static final long serialVersionUID = 1L;
    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

注意点

 /**
     *  read():
     *      读取到文件末尾返回值是 -1
     *  readLine():
     *      读取到文件的末尾返回值 null
     *  readObject():
     *      读取到文件的末尾 直接抛出异常
     *  如果要序列化的对象有多个,不建议直接将多个对象序列化到文件中,因为反序列化时容易出异常
     *      建议: 将要序列化的多个对象存储到集合中,然后将集合序列化到文件中
     */
Properties

Properties介绍

  • 是一个Map体系的集合类
  • Properties可以保存到流中或从流中加载
  • 属性列表中的每个键及其对应的值都是一个字符串

Properties作为Map集合的特有方法

方法名说明
Object setProperty(String key, String value)设置集合的键和值,都是String类型,底层调用 Hashtable方法 put
String getProperty(String key)使用此属性列表中指定的键搜索属性
Set stringPropertyNames()从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串

和IO流结合的方法

方法名说明
void load(Reader reader)从输入字符流读取属性列表(键和元素对)
void store(Writer writer, String comments)将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流

prop.properties

name=筱田优
age=30

Student.class

package cn.zzj;

import java.io.Serializable;

public class Student implements Serializable {
    private String name;
    private int age;
    //自定义序列号
    private static final long serialVersionUID = 1L;
    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Test.class

package cn.zzj;

import java.io.*;
import java.util.Properties;


public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //1.创建Properties集合,将本地文件中的数据加载到集合中
        Properties pro = new Properties();
        FileReader file = new FileReader("HotKey\\prop.properties");
        pro.load(file);
        //2.获取集合中的键值对数据,封装到学生对象中
        String name = pro.getProperty("name");
        int age = Integer.parseInt(pro.getProperty("age"));
        Student student = new Student(name, age);
        //3.创建序列化流对象,将学生对象序列化到本地文件中
        ObjectOutputStream ob = new ObjectOutputStream(new FileOutputStream("HotKey\\b.txt"));
        ob.writeObject(student);
        ob.close();

    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值