码迷,mamicode.com
首页 > 编程语言 > 详细

毕向东_Java基础视频教程第20天_IO流(1~4)

时间:2017-01-30 23:33:18      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:sdi   eth   rate   this   empty   jsp   cep   cut   documents   

第20天-01-IO流(File概述)

File类:

  1. 用来将文件或者文件夹封装成对象, 方便进行操作.

  2. File对象可以作为参数, 传递给流对象的构造函数.

  3. 流对象不能操作文件夹; 流对象不能操作文件的属性信息(rwx等), 只能操作文件的数据.

构造方法:

File(File parent, String child)   Creates a new File instance from a parent abstract pathname and a child pathname string.

File(String pathname)   Creates a new File instance by converting the given pathname string into an abstract pathname.

File(String parent, String child)   Creates a new File instance from a parent pathname string and a child pathname string.

package bxd;

import java.io.File;

public class FileDemo {

    public static void consMethod() {
        // 将a.txt封装成File对象, 可以将已有的或者未创建的"文件或文件夹"封装成对象.
        File file1 = new File("a.txt");
        File file2 = new File("/Users/Eric/Documents/IdeaProjects/Servlet_JSP/JDBCDemo/b.txt");
        String parent = "/Users/Eric/Documents/IdeaProjects/Servlet_JSP/JDBCDemo";
        File file3 = new File(parent, "c.txt");

        /* static String    separator
                The system-dependent default name-separator character, represented as a string for convenience.
        */
        File file4 = new File(File.separator + "abc" + File.separator + "xyz.txt");

        // 会以字符串形式打印出创建File对象时传入的参数,不会改变.
        sop("file1: " + file1);
        sop("file2: " + file2);
        sop("file3: " + file3);
        sop("file4: " + file4);
    }

    public static void main(String[] args) {
        consMethod();
    }

    public static void sop(Object object) {
        System.out.println(object);
    }
}

第20天-02-IO流(File对象功能-创建与删除)

boolean createNewFile()

  Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

boolean mkdir()

  Creates the directory named by this abstract pathname.

boolean mkdirs()

  Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.

static File createTempFile(String prefix, String suffix)

  Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.

static File createTempFile(String prefix, String suffix, File directory)

  Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.

boolean delete()

  Deletes the file or directory denoted by this abstract pathname.

void deleteOnExit()

  Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.

package bxd;

import java.io.File;
import java.io.IOException;

/*
File类常见方法:
1. 创建
    boolean createNewFile()  在指定位置创建文件. 如果创建成功则返回true; 如果创建失败或文件已经存在则返回false.
                             相比较, new一个输出流对象会马上创建文件, 且遇到文件已存在时会覆盖源文件.
    boolean mkdir()          创建指定的目录
    boolean mkdirs()         创建指定的目录, 包括所有所需但不存在的父目录
2. 删除
    boolean delete()         删除失败则返回false.
    void deleteOnExit()      文件有可能正被程序访问而无法删除, 此时可以将文件设置为程序退出时删除.
*/
public class FileDemo1 {

    public static void method_1() throws IOException {
        File file = new File("file.txt");
        sop("create file.txt: " + file.createNewFile());
    }

    public static void method_2() throws IOException {
        File dir = new File("dir_abc");
        dir.mkdir();
    }

    public static void method_3() {
        File file = new File("file.txt");
        sop("delete file.txt: " + file.delete());
    }

    public static void main(String[] args) throws IOException {
        method_2();
    }

    public static void sop(Object object) {
        System.out.println(object);
    }
}

第20天-03-IO流(File对象功能-判断)

boolean canExecute()   Tests whether the application can execute the file denoted by this abstract pathname.

boolean canRead()   Tests whether the application can read the file denoted by this abstract pathname.

boolean canWrite()   Tests whether the application can modify the file denoted by this abstract pathname.

boolean exists()     Tests whether the file or directory denoted by this abstract pathname exists.

boolean isDirectory()   Tests whether the file denoted by this abstract pathname is a directory.

boolean isFile()     Tests whether the file denoted by this abstract pathname is a normal file.

boolean isHidden()   Tests whether the file named by this abstract pathname is a hidden file.

boolean isAbsolute()   Tests whether this abstract pathname is absolute.

package bxd;

import java.io.File;
import java.io.IOException;

/*
File类常见方法:
3. 判断
    boolean exists()     判断文件/文件夹是否存在
    boolean canExecute() 判断文件是否可执行, 如果文件不存在也返回false
    boolean isFile()
    boolean isDirectory  判断File对象是否是文件/目录, 如果不存在也返回false
        所以判断时要注意区分: 是/否/不存在

    boolean isAbsolute() 判断File对象所对应的pathname是否是绝对路径(与文件/文件夹是否存在无关)
*/
public class FileDemo2 {

    public static void method_1() throws IOException {
        File file = new File("file.txt");
        sop("file.txt exists: " + file.exists());
        sop("file.txt canExcute: " + file.canExecute());
    }

    public static void method_2() {
        File file = new File("file.txt");
        sop("file.txt isFile: " + file.isFile());
        sop("file.txt isDirectory: " + file.isDirectory());
    }

    public static void main(String[] args) throws IOException {
        method_2();
    }

    public static void sop(Object object) {
        System.out.println(object);
    }
}

第20天-04-IO流(File对象功能-获取)

 

毕向东_Java基础视频教程第20天_IO流(1~4)

标签:sdi   eth   rate   this   empty   jsp   cep   cut   documents   

原文地址:http://www.cnblogs.com/echo1937/p/6358499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!