输入和输出(IO流)

File类的常用方法:

File对象的创建和部分方法使用:

        File file =new File("file\我是文本.txt");        
        System.out.println("相对路径是:"+file.getPath());
        //绝对路径
        System.out.println("绝对路径是:"+file.getAbsoluteFile());        
        System.out.println("文件名字是:"+file.getName());
        System.out.println("文件字符的长度:"+file.length());
        //判断是否是文件,是返回true
        System.out.println(file.isFile());
        File file2=new File("D:/自动创建.txt");
        //创建文件时 如果文件不存在则创建文件并返回true,如果文件存在则不再创建并返回false。
        boolean createNewFile = file2.createNewFile();
        System.out.println(createNewFile);

输出结果:

注:文件的绝对路径是由程序根据程序当前路径和文本相对路径拼接而成的(可见方法不够智能,需要注意)

流:

流是一组有序的数据序列

以先进先出方式发送信息的通道

字节流是 8 位通用字节流,字符流是 16 位 Unicode 字符流

输入输出流是相对于计算机内存来说的

FileInputStream和FileOutputStream的使用:

        //创建输入流
        FileInputStream fis=new FileInputStream("file\我是文本.txt");
        //创建输出流
        FileOutputStream fos=new FileOutputStream("D:/我也是文本.txt");
        StringBuilder su=new StringBuilder();
        byte[] bytes=new byte[1024];
        int num;
        //一次读取一个数组长度的字符
        while((num=fis.read(bytes))!=-1){
            String s=new String(bytes);
            //把文件中的内容写到一个可变字符串中
            su.append(s);
            //把文件中的内容写到另一个文件中,相当于复制文件
            fos.write(bytes, 0, num);        
        }
        //把文件中的内容打印出来            
        System.out.print(su);
        //关闭输入输出流,先开的后关
        fos.close();
        fis.close();

 FileReader和FileWriter的使用:

        FileReader fr=new FileReader("file\我是文本.txt");
//在文件内容后面添加 FileWriter fw
=new FileWriter("D:/自动创建.txt",true); String st="我爱你中国!"; StringBuilder sbu=new StringBuilder(); char[] chars=new char[1024]; int num; while((num=fr.read(chars))!=-1){ String s=new String(chars); sbu.append(s); fw.write(chars,0,num); } //可以直接写字符串 fw.write(st); System.out.print(sbu); fw.close(); fr.close();

 读写二进制文件:

 DataInputStream和DataOutputStream的使用:

        //创建读二进制文件对象
        DataInputStream din=new DataInputStream(new FileInputStream("file\皮卡丘.jpeg"));
        //创建写二进制文件对象,要写到的路径和图片名字
        DataOutputStream dou=new DataOutputStream(new FileOutputStream("D:\皮卡丘啊.jpg"));
        int num;
        byte[] bytes=new byte[1024];
        while((num=din.read(bytes))!=-1){
            //复制图片
            dou.write(bytes,0,num);
        }
        dou.close();
        din.close();

序列化和反序列化:

 序列化和反序列化的应用场景:

序列化类的创建:

//实现Serializable接口代表该类可以序列化
public class Student implements Serializable{
    //序列化版本
    private static final long serialVersionUID = 1L;
    
    private String name;
    private char sex;
    //transient关键字可以使该变量不被序列化
    private transient int age;
    
    public Student(String name,char sex,int age){
        this.name=name;
        this.sex=sex;
        this.age=age;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public char getSex() {
        return sex;
    }
    public void setSex(char sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String toString(){
        return "Student [name=" + name + ", age=" + age + ",sex="+sex+"]";
    }

}

序列化和反序列化的使用:

public class SerializableTest {
    
    public static void main(String[] args) throws Exception {
        Student stu=new Student("张三",'男',21);
        //序列化,把Student对象以字节存放到记事本中
        ObjectOutputStream ops=new ObjectOutputStream(new FileOutputStream("file\我是文本.txt"));
        ops.writeObject(stu);
        ops.close();
        //反序列化,把记事本中的数据转换为对象输出
        ObjectInputStream obs=new ObjectInputStream(new FileInputStream("file\我是文本.txt"));
        Student stu1=(Student) obs.readObject();
        obs.close();
        System.out.print(stu1);

    }

}

运行结果:也可见,变量age没有被序列化

原文地址:https://www.cnblogs.com/TFE-HardView/p/11011066.html