IO 章节 学习-文件夹操作练习
今天开始学习 IO 类 和 方法
首先 了解 File 类
首先看一下 它的四个 static field
Modifier and Type | Field 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公司的规范真蛋疼
文件夹操作代码加注释笔记:
1 import java.io.*; 2 3 /** 4 * Created by wojia on 2017/6/28. 5 */ 6 public class fileDemo { 7 public static void main(String args[]) throws IOException { 8 /** three type of constructor */ 9 File f1 = new File( "c:/abc.txt" ); 10 File f2 = new File( "c:/" + "abc.txt" ); 11 File dir = new File( "c:/" ); 12 File f3 = new File(dir , "abc.txt"); 13 System.out.println(f1); 14 System.out.println(f2); 15 System.out.println(f3); 16 17 /**操作path 18 * 1. 19 * 2. 20 * 3. 21 * */ 22 new test().doWork(); 23 } 24 25 } 26 class test{ 27 public void doWork () throws IOException { 28 /**一些文件操作*/ 29 //查询文件是否存在 若不存在 创建 30 File dir = new File( "E:/abc" ); 31 File file = new File( dir , "123.txt" ); 32 /**warning !! 33 * The problem is that a file can't be created unless the entire containing path already exists 34 * its immediate parent directory and all parents above it. 35 * */ 36 if (!file.exists()) { 37 try { 38 //because the abc directory was not exist 39 //create the parent dir of the file first 40 dir.mkdir(); 41 file.createNewFile(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 //make sure that the operation above make sense 47 System.out.println(file.exists()); 48 49 // 50 /** 51 * task:delete the dir and the txt file 52 * warning!! 53 * 官方解释: 54 * Deletes the file or directory denoted by this abstract pathname. If 55 * this pathname denotes a directory, then the directory must be empty in 56 * order to be deleted. 57 * 58 * */ 59 60 //these steps should be action orderly 61 file.delete();//output true 62 //after deleting the txt file , the directory exists 63 System.out.println(dir.exists()); 64 dir.delete(); 65 //after deleting the empty dir , the directory does not exist 66 System.out.println(dir.exists());//output false 67 /** 68 * 归纳起来就是: 69 * 1.由于系统的规则,文件在被创建时 其parent-path 必须先创建 存在 才能创建对应path下的file 70 * 否则出错 error 71 * 2.delete操作可识别 是 文件还是文件夹(denote a directory) 再删除 72 * 但删除一个文件夹时 必须保证 文件为空 否则 不会报错 但文件夹不会被正常删除! 73 * */ 74 75 /**create temp file*/ 76 File tempfile = File.createTempFile( "Tommy",".xxx",new File( "E:/" ) ); 77 //delete the temp file using the method deleteOnExist(); 78 tempfile.deleteOnExit(); 79 80 /**文件夹 操作 81 * 主要是 mkdir and mkdirs 82 * 下面不作示范 记住 mkdir只是创建一级目录 若其parent path 不存在 则创建失败 83 * mkdirs 则创建子目录和父目录 84 * */ 85 86 //***** 87 88 /** 89 * list & listFile 按需选取 90 * */ 91 92 /** 93 * <p></>now we get a deeper look at the method list()(similar to listFile() , so we just talk about one of them) 94 * official doc : 95 * @return >>> An array of strings naming the files and directories in the 96 * directory denoted by this abstract pathname. The array will be 97 * empty if the directory is empty. Returns {@code null} if 98 * this abstract pathname does not denote a directory, or if an 99 * I/O error occurs. 100 * 就是说 如果指向的路径是个文件夹,而且为空文件夹,则return一个空String[]数组, 101 * 这个可以用一个空文件夹路径和 String.length 来证明 102 * */ 103 //这里 E:/test 是一个空文件夹(基于我的系统) 104 File fs = new File("E:/test"); 105 //list all file 106 String[] filename = fs.list(); 107 //because the test directory is empty , the filename array is empty,its length is 0 108 System.out.println(filename.length); 109 for (String ele:filename 110 ) { 111 System.out.println(ele); 112 } 113 114 if(fs.listFiles()== null){ 115 System.out.println("ddddd"); 116 } 117 118 119 //an not empty directory 120 File fs2 = new File("E:/"); 121 122 //list() returns a string array 123 String[] name = fs.list(); 124 for (String ele:name) { 125 //note that String does not has isFile() method 126 System.out.println(ele); 127 } 128 129 //listFiles returns a File array, File.toString>>>abstract path name 130 File[] name2 = fs2.listFiles(); 131 for (File ele:name2) { 132 System.out.println(ele + ">>>" + ele.isFile()); 133 134 } 135 136 137 } 138 }