IO 章節 學習
阿新 • • 發佈:2017-06-30
問題 acl void ioe represent modifier paths har 默認
今天開始學習 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公司的規範真蛋疼
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 章節 學習