您的位置:首页 > 编程语言 > PHP开发

WriteOutputStream

2016-07-07 11:08 323 查看
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());

            }

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java IO OutputStream