WriteOutputStream

来源:互联网 发布:生化危机7ps4淘宝 编辑:程序博客网 时间:2024/04/29 19:31
ObjectOutputStream oos = null;
        try {        
            oos = new ObjectOutputStream(new FileOutputStream("d:\\file.txt"));
            Students student = new Students(50, "dsfhgj", 20);
            Students student2 = new Students(123, "赵三", 25);
            oos.writeObject(student);
            oos.writeObject(student2);
            ObjectInputStream objectReader = new ObjectInputStream(
                    new FileInputStream("d:\\file.txt"));
            for (int i = 0; i < 3; i++) {
                System.out.println(objectReader.readObject());
            }
        } catch (Exception e) {
            
        } finally {
            oos.close();
        }

        File file = new File("d:\\", "file");
        System.out.println("文件名" + file.getName());
        System.out.println("路径" + file.getParent());
        System.out.println("绝对路径" + file.getAbsolutePath());
        System.out.println("判断文件是否存在" + file.exists());
        System.out.println(file.isDirectory() ? "目录" : "文件");
        System.out.println(file.isFile() ? "普通" : "命名管道");
        if (file.canRead()) {// 是否能读
            System.out.println("可读文件");
        } else {
            System.out.println("非可读文件");
        }
        if (file.canWrite()) {
            System.out.println("可写文件");
        } else {
            System.out.println("非可写文件");
        }
        System.out.println(file.lastModified());// 文件最后修改时间()
        File file2 = new File("d:\\a.txt");
        file2.createNewFile();
        File file3 = new File("d:\\pr文件");
        showDirs(file3);
    }

    /**
     * 创建文件
     *
     * @param file
     * @throws IOException
     */
    public static void create(File file) throws IOException {
        if (!file.exists()) {
            file.createNewFile();
        }
    }

    /**
     * 删除文件
     *
     * @param file
     * @throws IOException
     */
    public static void delete(File file) throws IOException {
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * 显示文件路径
     *
     * @param file
     * @throws IOException
     */
    public static void showDirs(File file) throws IOException {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file2 : files) {
                System.out.println(file2.getName());
            }
        }
0 0
原创粉丝点击