首页 > 代码库 > IO 章节 学习

IO 章节 学习

今天开始学习 IO 类 和 方法

首先 了解 File 类

首先看一下 它的四个 static field

Fields
Modifier and TypeField and Description
static String pathSeparator
The system-dependent path-separator character represented as a string for convenience.
static char pathSeparatorChar
The system-dependent path separator character.
static String separator
The system-dependent default name-separator character represented as a string for convenience.
static char separatorChar
The system-dependent default name-separator character.

separtatorChar 和 separator 其实作用一样 但是一个是返回 char类型 一个是返回 String 类型 的 separator  就是所谓的属性分隔符(在windows 里 默认为 " \ "在winJava 中由于转义问题,使用“\\”; linux 为"/")

path separator 和 path separatorChar 作用类似,同上,就是所谓的路径分隔符 ,win中默认为 “;”,而 linux中为“:”

至于为何 path separator 叫做 属性分隔符 而不是叫做 路径分隔符  我暂时不懂  sun公司的规范真蛋疼

 

import java.io.*;

/**
 * Created by wojia on 2017/6/28.
 */
public class fileDemo {
    public static void main(String args[]) throws IOException {
        /** three type of constructor */
        File f1  = new File( "c:/abc.txt" );
        File f2  = new File( "c:/" + "abc.txt" );
        File dir = new File( "c:/" );
        File f3  = new File(dir , "abc.txt");
        System.out.println(f1);
        System.out.println(f2);
        System.out.println(f3);

        /**操作path
         * 1.
         * 2.
         * 3.
         * */
        new test().doWork();
    }

    }
    class test{
        public void doWork () throws IOException {
            /**一些文件操作*/
            //查询文件是否存在 若不存在 创建
            File dir  = new File( "E:/abc" );
            File file = new File( dir , "123.txt" );
            /**warning !!
             * The problem is that a file can‘t be created unless the entire containing path already exists
             * its immediate parent directory and all parents above it.
             * */
            if (!file.exists()) {
                try {
                    //because the abc directory was not exist
                    //create the parent dir of the file first
                    dir.mkdir();
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //make sure that the operation above make sense
            System.out.println(file.exists());

            //
            /**
             * task:delete the dir and the txt file
             * warning!!
             * 官方解释:
             * Deletes the file or directory denoted by this abstract pathname.  If
             * this pathname denotes a directory, then the directory must be empty in
             * order to be deleted.
             *
             * */

            //these steps should be action orderly
            file.delete();//output true
            //after deleting the txt file , the directory exists
            System.out.println(dir.exists());
            dir.delete();
            //after deleting the empty dir , the directory does not exist
            System.out.println(dir.exists());//output false
            /**
             * 归纳起来就是:
             * 1.由于系统的规则,文件在被创建时 其parent-path 必须先创建 存在  才能创建对应path下的file
             * 否则出错 error
             * 2.delete操作可识别 是 文件还是文件夹(denote a directory) 再删除
             * 但删除一个文件夹时 必须保证 文件为空 否则 不会报错 但文件夹不会被正常删除!
             * */

            /**create temp file*/
            File tempfile = File.createTempFile( "Tommy",".xxx",new File( "E:/" ) );
            //delete the temp file using the method deleteOnExist();
            tempfile.deleteOnExit();

            /**文件夹 操作
             * 主要是 mkdir and mkdirs
             * 下面不作示范  记住 mkdir只是创建一级目录  若其parent path 不存在 则创建失败
             * mkdirs 则创建子目录和父目录
             * */
            
            //*****
            
            /**
             * list listFile 按需选取
             * */
            //String 仅仅是文件名
            File fs = new File("E:/");
            String[] name = fs.list();
            for (String ele:name) {
                System.out.println(ele);
            }
            //File toString 包括地址
            File fs2 = new File("E:/");
            File[] name2 = fs2.listFiles();
            for (File ele:name2) {
                System.out.println(ele);
            }
        }
    }

 

IO 章节 学习