栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

IO流----对象流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

IO流----对象流

将对象在内存中的二进制,映射到文件中去

objectInoutStream  :二进制字节流输入

ObjectOutStream :二进制字节流输出

writeobject:字符输入

readobject:字符输出

运用中的关键字entity:实体

对象输出流练习:

java:

public class Student implements Serializable {
    String name;
    String gender;

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

java2:

将对象流输出到文件中去

public class StudentYingshePractice {
    public static void main(String[] args) throws IOException {
        OutputStream os = new FileOutputStream("D:\java\eclipse\学生.dat");
        ObjectOutputStream is = new ObjectOutputStream(os);
        Student student = new Student();
        student.setName("张三");
        student.setGender("male");
        is.writeObject(student);
        is.close();
        os.close();
    }
}
编码集 字符打印输出流 PrintWrite:常用的system就是OPrintWriter类型的 转换流

流的类型为字节流,但是能够确保读到的是字符,所以可以将字节流,转换成字符流

public class TarnferStreamPractice {
    public static void main(String[] args) throws IOException {
        InputStream is=new FileInputStream("D:\java\java2021-10-29\02-代码\J2104-1029-thread\src\com\mobiletrain\BaoziPractice.java");
        InputStreamReader oi = new InputStreamReader(is);
        BufferedReader ig = new BufferedReader(oi);
        String len;
        while ((len=ig.readLine())!=null){
            System.out.println(len);
        }
        ig.close();
        oi.close();
        is.close();
    }
}

File

public class FilePractice {
    public static void main(String[] args) throws IOException {
        // File类
        // 用于表示一个文件或文件夹
        File file = new File("/Users/LEAF/Downloads/newdir/a/b/c/newfile.txt");

        // createNewFile
        // create: 创建
        // new: 新的
        // file: 文件
        // file.createNewFile();

        // mkdir
        // make: 制作
        // directory: 目录
        // 只能创建一级目录
        // 如果创建不了,不报错
        // file.mkdir();

        // 可以创建多级父子目录
        // file.mkdirs();

        // delete
        // 删除
        file.delete();

        // exists
        // exists: 直译: 存在
        // 返回值: true/false
        // 即:返回true,代表文件存在;返回false,代表文件不存在
        System.out.println(file.exists());

        // getAbsolutePath
        // getName
        // getParent
        // isDirectory
        // isFile
        // length
        // listFiles
        // renameTo
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424724.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号