Java文件操作

1.File类

1.构造方法
File(String name):表示生成一个含有虚拟路径的文件类(官方解释为:Creates a new File instance by converting the given pathname string into an abstract pathname.)。大概意思就是根据给定路径名转换为一个虚拟的路径名,将该虚拟路径的信息封装到File对象中。
2.成员方法

  • createNewFile():如果虚拟路径中的文件不存在则将该文件创建,否则返回false。
  • mkdir():如果虚拟路径中的文件夹不存在则将该文件夹创建,否则返回false。
  • isFile():判断该虚拟文件所对应文件是否为文件。
  • isDirctory():判断该虚拟文件所对应文件是否为文件夹。

3.File类并不负责访问文件内容,以及对内容进行操作,其负责的是“外围操作”。

2.字节流

1.FileInputStream

  • 构造方法:FileInputStream(File file):根据传进来的File对象创建一个FileInputstream对象,若该File对象对应文件不存在,则抛出异常。其余构造方法大同小异。
  • 作用:用于从磁盘文件中读入数据到内存。
  • 读取数据方法:read()或read(byte[] bytes)。其中第一个为读取一个字节,返回值为读取字节的ASCII码值;第二个为读取多个字节,并将读取结果放入字节数组中,返回值为读取的字节数。

2.FileOutputStream

  • 构造方法
    FileOutputStream(File file):根据传进来的File对象创建一个FileOutputstream对象,若File对象对应文件不存在,则创建该文件。注意:该构造方法默认的写操作为覆盖,即每次写入内容都会将原来写入内容覆盖,若不想覆盖,则需再添加一个参数true,表示写入内容时,进行的是追加操作。其余构造方法大同小异。
  • 作用:用于将内存中数据存入磁盘文件中。
  • 存入数据方法:write()或write(byte[] bytes,int pos,int len)。第一个表示写入一个字节。第二个表示写入一个字节数组,从字节数组的pos处开始写入,共写入len个字节。
    +flush():用于将缓存中数据
    3.复制文件
public class FileTest01 {
    public static void main(String[] args) {
        try {
            FileOutputStream out = new FileOutputStream("test02file.txt",true);
            FileInputStream in = new FileInputStream("test01file.txt");
            int len;
            byte[] bytes = new byte[1024];//临时数组,用于临时保存数据
            while((len=in.read(bytes))!=-1){//-1表示读取完毕
                out.write(bytes,0,len);
            }
            out.flush();
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

字符流

1.字符流与字节流的不同之处在于,字符流是一个字符一个字符地操作,而字节流是一个字节流一个字节流地操作*。字符流通常用于文本文件的操作,其构造器和方法与字节流大同小异。
2.复制文件

public class FileTest01 {
    public static void main(String[] args) {
        try {
            FileWriter out = new FileWriter("test02file.txt",true);
            FileReader in = new FileReader("test01file.txt");
            int len;
            char[] chars = new char[1024];
            while((len=in.read(chars))!=-1){
                out.write(chars,0,len);
            }
            out.flush();
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

对象的序列化与反序列化

1.序列化:利用流将对象存储到文件中。
2.反序列化:利用流将文件中的对象返回到内存中。
3.被序列化对象或被反序列化对象满足条件:实现Serializable接口,该接口中没有任何方法,只是一个标志接口,标识了该对象可以进行序列化和反序列化。
4.serialVersionUID:系统默认为每一个实现了Serializable接口的类生成一个
serialVersionUID,该serialVersionUID是唯一的,用于标识唯一类,若改变类的结构,JVM的运行时环境运行机制会自动重新分配serialVersionUID。反序列化时,JVM运行时环境会检查serialVersionUID是否相同,若不同,则抛出异常。

5.测试代码

package testfile;

import java.io.*;

public class TestStudent {
    public static void main(String[] args) {
        writeObject();
        readObject();
    }
    public static void writeObject(){
        try {
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.txt"));
            Student student = new Student("Tom",18,"男");
            out.writeObject(student);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readObject(){
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("student.txt"));
            Student student =(Student) in.readObject();
            in.close();
            System.out.println(student);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

其中Student的代码

package testfile;

import java.io.Serializable;

public class Student implements Serializable {

    private String name;
    private int age;
    private String sex;

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

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

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

控制台结果
控制台结果一
改变Student的属性,并反序列化后的结果:
改变类结构的结果
结果表明,序列化和反序列化的并不是同一个类的对象。若想改变类的结构,但不影响反序列化,则可以自己为需要序列化的对象添加一个名为 serialVersionUID的成员(IDEA可通过ALT + ENTER键进行添加,之不许需要进行设置),并初始化。

6.transient关键字:被该关键字修饰的属性不会被序列化。此外被static修饰的属性也不会被序列化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值