Loading

java基础-文件

概述

  • 它是文件和目录路径名的抽象表示
  • 文件和目录是可以通过File封装成对象的
  • 对于File而言,其封装的并不是一个真正存在的文件,仅仅是一个路径名而已。它可以是存在的,也可以是不存在的。将来是要通过具体的操作把这个路径的内容转换为具体存在的

构造方法

  • File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的File实例
  • File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的File实例心
  • File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的File实例
package file.demo1;

import java.io.File;

public class Test {
    public static void main(String[] args) {
        File f1 = new File("E:\\IDE\\test\\java.txt");
        System.out.println(f1);

        File f2 = new File("E:\\IDE\\test", "java.txt");
        System.out.println(f2);

        File f3 = new File("E:\\IDE\\test");
        File f4 = new File(f3, "java.txt");
        System.out.println(f4);
    }
}
/**
E:\IDE\test\java.txt
E:\IDE\test\java.txt
E:\IDE\test\java.txt
**/

创建功能

  • public boolean createNewFile() 当具有该名称的文件不存在时,创建一个由该抽象路径名命名的新空文件
  • public boolean mkdir() 创建由此抽象路径名命名的目录
  • public boolean mkdir() 创建由此抽象路径名命名的目录,包括任何必需但不存在的父目录
package file.demo1;

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

public class Test2 {
    public static void main(String[] args) throws IOException {
        //创建对应文件
        File f1 = new File("E:\\IDE\\test\\java.txt");
        System.out.println(f1.createNewFile());

        //创建对应文件夹
        File f2 = new File("E:\\IDE\\test\\java");
        System.out.println(f2.mkdir());

        //创建对应多级目录
        File f3 = new File("E:\\IDE\\java\\test");
        System.out.println(f3.mkdirs());
    }
}
//创建成功成返回true 

判断和获取功能

  • public boolean isDirectory() 测试此抽象路径名表示的File是否为目录
  • public boolean isFile() 测试此抽象路径名表示的File是否为文件
  • public boolean exists() 测试此抽象路径名表示的File是否存在
  • public String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串
  • public String getPath() 将此抽象路径名转换为路径名字符串
  • public String getName() 返回由此抽象路径名表示的文件或目录的名称
  • public String[] list() 返回此抽象路径名表示的目录中的文件和目录的名称字符串数组
  • public File[] listFile() 返回此抽象路径名表示的目录中的文件和目录的File对象数组
  • public boolean delete() 删除由此抽象路径名表示的文件或目录
package file.demo1;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.io.File;

public class Test3 {
    public static void main(String[] args) {
        File f = new File("test\\java.txt");
        //判断抽象路径名是否为目录
        System.out.println(f.isDirectory());
        //判断抽象路径名是否为文件
        System.out.println(f.isFile());
        //判断抽象路径名是文件是否存在
        System.out.println(f.exists());
        //抽象路径名的绝对路径名字符串
        System.out.println(f.getAbsoluteFile());
        ///将抽象路径名转为文件路径
        System.out.println(f.getPath());
        //返回抽象路径名的文件名或目录名
        System.out.println(f.getName());

        //回此抽象路径名表示的目录中的文件和目录的名称字符串数组
        File f2 = new File("E:\\IDE\\test");
        String[] a = f2.list();
        for (String i :  a){
            System.out.print(i+" ");
        }
        System.out.println();

        //返回此抽象路径名表示的目录中的文件和目录的File对象数组
        File[] files = f2.listFiles();
        for (File file : files){
            System.out.print(file+" ");
        }
    }
}
/**
false
false
false
E:\java\study\project\test\java.txt
test\java.txt
java.txt
java java.txt 
E:\IDE\test\java E:\IDE\test\java.txt 
**/

字节流

IO流

  • IO:输入/输出(Input/Output)
  • 流:是一种抽象概念,是对数据传输的总称。也就是说数据在设备间的传输称为流,流的本质是数据传输IO流就是用来处理设备间数据传输问题的
  • 常见的应用:文件复制;文件上传;文件下载

IO流分类:

  • 按照数据的流向 输入流:读数据 输出流:写数据

  • 按照数据类型来分

    字节流 字节输入流;字节输出流

    字符流 字符输入流;字符输出流

字节流抽象基类

  • lnputStream: 这个抽象类是表示字节输入流的所有类的超类
  • OutputStream: 这个抽象类是表示字节输出流的所有类的超类
  • 子类条特点: 子类名称都是以其父类名作为子类名的后缀

字节流写数据

FileOutputStream(String name): 创建文件输出流以指定的名称写入文件

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

  • 创建字节输出流对象(调用系统功能创建了文件,创建字节输出流对象,让字节输出流对象指向文件)
  • 调用字节输出流对象的写数据方法
  • 释放资源(关闭此文件输出流并释放与此流相关联的任何系统资源)
package file.demo1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Test4 {
    public static void main(String[] args) throws IOException {
        //创建文件输出流以指定的名称写入文件
        FileOutputStream fos = new FileOutputStream("fileTest\\fileOuts.txt");
        /*
            1.调用系统功能创建了对象
            2.创建了字节流输出流对象
            3.让字节输出流对象指向创建的文件
         */
        fos.write(97);
        fos.write(98);
        fos.write(99);

        byte[] bys = "abc".getBytes(StandardCharsets.UTF_8);
        fos.write(bys);

        fos.write(bys,1,1);
        fos.close(); //关闭此文件输出流并释放与此流相关联的任何系统资源。

    }
}
/**
abcabcb
**/
  • void write(int b):一次写一个数据
  • void write(byte[] b): 一次写一个字节数组数据
  • void write(byte[] b, int off, int len): 一次写一个字节数的部分数据

FileOutputStream("fileTest\\fileOuts.txt", true) 追加写入

字节流读数据

FileInputStream:从文件系统中的文件获取输入字节
FileInputStream(String name)。通过打开与实际文件的连接来创建一个FileInputStream ,该文件由文件系统中的路径

package file.demo1;

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

public class Test5 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("fileTest\\fileOuts.txt");

        int b;
        while ((b = fis.read()) != -1){
            System.out.println(b+" "+ (char)b);
        }

    }
}

字节缓冲流

该类实现缓冲输出流。通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致底层系统的调用。

  • BufferOutputStream: 该类实现缓冲输出流。通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致底层系统的调用
  • BufferedInputStream:创建BufferedInputStream将创建一个内部缓冲区数组。当从流中读取或跳过字节时,内部级冲区将根据需要从所包含的输入流中重新填充,一次很多字节

复制文件的四中方法比较

package file.demo2;

import java.io.*;

public class Copy {
    public static void main(String[] args) throws IOException{
        long t1 = System.currentTimeMillis();

        //fun1("fileTest\\test.jpg", "fileTest\\1.jpg");   56772ms
        //fun2("fileTest\\test.jpg", "fileTest\\1.jpg");   71ms
        //fun3("fileTest\\test.jpg", "fileTest\\1.jpg");   112ms
        //fun4("fileTest\\test.jpg", "fileTest\\1.jpg");   16ms

        long t2 = System.currentTimeMillis();
        System.out.println(t2-t1);
    }

    public static void fun1(String s1, String s2) throws IOException {
        FileInputStream fis = new FileInputStream(s1);
        FileOutputStream fos = new FileOutputStream(s2);

        int b;
        while ((b = fis.read()) != -1){
            fos.write(b);
        }
        fis.close();
        fos.close();
    }

    public static void fun2(String s1, String s2) throws IOException {
        FileInputStream fis = new FileInputStream(s1);
        FileOutputStream fos = new FileOutputStream(s2);

        byte[] bys = new byte[1024];
        int len;
        while ((len = fis.read(bys)) != -1){
            fos.write(bys,0,len);
        }
        fis.close();
        fos.close();
    }

    public static void fun3(String s1, String s2) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(s1));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(s2));

        int b;
        while ((b = bis.read()) != -1){
            bos.write(b);
        }
        bis.close();
        bos.close();
    }

    public static void fun4(String s1, String s2) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(s1));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(s2));

        byte[] bys = new byte[1024];
        int len;
        while ((len = bis.read(bys)) != -1){
            bos.write(bys,0,len);
        }
        bis.close();
        bos.close();
    }
}

字符流

字符流抽象基类

  • Reader:字符输入流的抽象类
  • Writer:字符输出流的抽象类

字符流中和编码解码问题相关的两个类:

  • InputStreamReader
  • OutputStreamWriter

字符流写数据

  • 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) 写一个字符串的一部分
package file.demo3;

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

public class Test {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("fileTest\\1.txt"));

        osw.write(97);
        osw.write(98);
        osw.flush();  //刷新流,还可以继续写数据

        char[] chs = {'a', 'b', 'c'};
        osw.write(chs);
        osw.flush();

        osw.write("aaa");
        osw.flush();

        osw.write("bbb", 0, 1);
        osw.flush();

        osw.close();  //关闭流,释放资源,但是在关闭之前会先刷新流。一旦关闭,就不能再写数据

    }
}
/**
ababcaaab
**/

字符流读数据

  • int read() 一次读一个字符数据
  • int read(char[] buf) —次读一个字符数组数据
package file.demo3;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test2 {
    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream("fileTest\\1.txt"));

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

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

字符缓存流

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

import java.io.*;

public class Test3 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("fileTest\\1.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("fileTest\\2.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:
    public String readLine():读一行文字。结果包含行的内容的字符串,不包括任何行终止字符,如果流的结尾已经到达,则为null
package file.demo3;

import java.io.*;

public class Test4 {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("fileTest\\4.txt"));

        for (int i = 0; i < 3; i++) {
            bw.write("hello");
            bw.newLine();
        }
        bw.close();

        BufferedReader br = new BufferedReader(new FileReader("fileTest\\4.txt"));

        String line;
        while ((line= br.readLine()) != null){
            System.out.println(line);
        }
        br.close();
    }

}

实验

把ArrayList集合中的学生数据写入到文本文件。要求:每一个学生对象的数据作为文件中的一行数据

package file.demo3;

public class Student {
    private String id;
    private String name;
    private int age;

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    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{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
package file.demo3;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class SetToFile {
    public static void main(String[] args) throws IOException {
        ArrayList<Student> arr = new ArrayList<Student>();

        Student s1 = new Student("test001", "Jack", 18);
        Student s2 = new Student("test002", "Bob", 20);
        Student s3 = new Student("test003", "Tim", 16);

        arr.add(s1);
        arr.add(s2);
        arr.add(s3);

        BufferedWriter bw = new BufferedWriter(new FileWriter("fileTest\\list.txt"));
        for (Student a : arr) {
            StringBuilder sb = new StringBuilder();
            sb.append(a.getId()).append(",").append(a.getName()).append(",").append(a.getAge());
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
    }
}

list.txt

test001,Jack,18
test002,Bob,20
test003,Tim,16

把文本文件中的数据读取到集合中,并遍历集合。要求:文件中每一行数据是一个学生对象的成员变量值

package file.demo3;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class FileToSet {
    public static void main(String[] args) throws IOException {
        BufferedReader bw = new BufferedReader(new FileReader("fileTest\\list.txt"));
        ArrayList<Student> arr = new ArrayList<Student>();

        String line;
        while ((line = bw.readLine()) != null){
            String[] str = line.split(",");

            Student s = new Student(str[0], str[1], Integer.parseInt(str[2]));
            arr.add(s);
        }
        for (Student a : arr){
            System.out.println(a.toString());
        }
    }
}
/**
Student{id='test001', name='Jack', age=18}
Student{id='test002', name='Bob', age=20}
Student{id='test003', name='Tim', age=16}
**/

将文件夹复制到对应目录

package file.test;

import java.io.*;

public class CopyFiles {
    public static void main(String[] args) throws IOException{
        File srcFile = new File("E:\\IDE\\test\\");
        File desFile = new File("E:\\");

        copyFolder(srcFile, desFile);
    }

    private static void copyFolder(File srcFile, File desFile) throws IOException{
        String srcName = srcFile.getName();
        if (srcFile.isDirectory()){
            File newFolder = new File(desFile, srcName);
            if (!newFolder.exists()){
                newFolder.mkdir();
            }

            File[] FileArr = srcFile.listFiles();
            for (File file:FileArr) {
                System.out.println(file);
                copyFolder(file, newFolder);
            }

        }else {
            File newFile = new File(desFile, srcName);
            copyFile(srcFile, newFile);
            System.out.println("复制:" + srcName);
        }
    }

    private static void copyFile(File f1, File f2) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f1));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f2));

        byte[] bys = new byte[1024];
        int len;
        while ((len=bis.read(bys)) != -1){
            bos.write(bys,0,len);
        }
        bis.close();
        bos.close();
    }
}

对象序列化

序列化

  • 对象序列化:就是将对象保存到磁盘中,或者在网络中传输对象
  • 这种机制就是使用一个字节序列表示一个对象,该字节序列包含:对象的类型、对象的数据和对象中存储的属性等信息字节序列写到文件之后,相当于文件中持久保存了一个对象的信息
  • 反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化
  • 要实现序列化和反序列化就要使用对象序列化流和对象反序列化流:
    对象序列化流:ObjectOutputStream
    对象反序列化流:ObjectlnputStream

将Java对象的原始数据类型和图形写入OutputStream。可以使用ObjectInputStream读取((重构)对象。可以通过使用流的文件来实现对象的持久存储。如果流是网络套接字流,则可以在另一个主机上或另一个进程中重构对象

package file.demo4;

import java.io.*;
import java.io.ObjectOutputStream;

public class Test {
    public static void main(String[] args) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("fileTest\\oos.txt"));
        Student s = new Student("Bob", 18);
        oos.writeObject(s);
        oos.close();
    }
}

但是报错了

Exception in thread "main" java.io.NotSerializableException

要想实现序列化 必须要实现 java.io.Serializable 接口的类启用。不实现此接口的类将不会使任何状态序列化或反序列化。

package file.demo4;

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;
    }
}

Serializable是一个标记接口,实现该接口,不需要重写任何方法。

反序列化

package file.demo4;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Test2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("fileTest\\oos.txt"));

        Object o = ois.readObject();
        Student s = (Student)o;
        System.out.println(s.getName() + ":" + s.getAge());
    }
}
posted @ 2021-07-30 16:40  Th0r  阅读(38)  评论(0编辑  收藏  举报