# IO ## File File為一種類別,new物件時須給檔案路徑,如下: File dir =new File("C:\\java"); ### 常用方法 1. getName();//回傳檔案名稱 2. listFile();//將所有檔案及目錄回傳至File[]陣列中,因此常見寫法 `File[]x=dir.listFile();` 3. isDirectory();//回傳是否為目錄 4. lastModified();//回傳最後修改時間,回傳值為long型態,可藉由下面語法轉為時間 `Date modifiedDate = new Date(time); SimpleDateFormat format = new SimpleDateFormat("yyyy,MM,dd a hh:mm:ss");//a是上午或下午 System.out.println("\t修改時間:"+format.format(modifiedDate)); ` 5. length();//檔案的大小 ### FileInputStream :專門讀取檔案用 語法:FileInputStream fls = new FileInputStream(String 檔案路徑或是File物件); 避免找不到檔案,因此必須throws FileNotFoundException #### 常用方法 1. read();//會有游標去重頭讀一個byte,若到底會回傳-1,讀完後為ASCII碼,必須透過轉型為char來顯示 2. close(); //關閉檔案,並且一定要執行,不然檔案會被鎖住,其他程式無法讀寫!!!但通常可以直接在try後面加()去寫new物件,宣告在try()裡面的物件執行結束後會自動close。語法如下: ``` try (FileInputStream fls = new FileInputStream("C:\\java\\io_1.txt"); BufferedInputStream bis = new BufferedInputStream(fls)) { ......... } catch{.....} ``` ### InputStreamReader :可指定讀的檔案編碼 語法: InputStreamReader isr = new InputStreamReader(InputStream,"編碼類別");//UFF8或是MS950 ### BufferedReader : 一次可以讀一行 語法: BufferedReader br = new BufferedReader(isr);// #### 方法 1. readLine();一次讀一行 ```//讀檔案中 多行 String line ; while(( line=br.readLine())!=null) { System.out.println(line); } ``` ### BufferedInputStream : 搭配FileInputStream使用,一次載入多一點資料到緩衝區,減少讀取次數加快運作效能。 語法:BufferedInputStream bis =new BufferedInputStream(InputStream in,int size); #### 常用方法 1. read();// ### FileOutputStream : 文字寫入檔案 語法:FileOutputStream fos =new FileOutputStream(String 檔案路徑或是File物件); #### 常用方法 1. write(byte b);//寫一個byte進檔案 2. write(byte []);//寫多個byte進檔案 3. getByte(); //把文字變byte ``` String content ="中文ABC"; byte[] bytes = content .getBytes(); fos.write(bytes); ``` ### BufferedOutputStream : 搭配FileOutputStream使用,一次載入多一點資料到緩衝區,減少寫入次數加快運作效能。 語法:BufferedOutputStream bos =new BufferedOutputStream(InputStream in,int size); #### 常用方法 1. write(); ### OutputStreamWriter :可指定寫入的檔案編碼 語法: OutputStreamWriter osw = new OutputStreamWriter(InputStream,"編碼類別");//UFF8或是MS950 ### BufferedWriter : 一次可以寫一行 語法: BufferedWriter bw = new BufferedWriter(osw) #### 方法 1. write();寫String進檔案 ## 實作:讀取路徑下的csv檔案文字,並把他印出 ``` try (FileInputStream fis = new FileInputStream("C:\\java\\Employees.csv"); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr);) { String line; boolean first = true; while ((line = br.readLine()) != null) { if (first == true) { first = false; continue; } else { System.out.println(line); } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ``` ## 實作:到對方伺服器-> Inputstream-> read ->資料讀到java ### URL: 到對方伺服器,並透過 ``` URL url = new URL("https://attach.mobile01.com/attach/202107/mobile01-43e3df6efe1826f7b6fe4a5222dbba4a.jpg"); try( InputStream is = url.openStream(); FileOutputStream fos= new FileOutputStream("c:\\java\\practice.jpg"); BufferedInputStream bis=new BufferedInputStream(is); BufferedOutputStream bos = new BufferedOutputStream(fos); ){ byte[] buffer=new byte[1024]; int length=0; while( ( length=bis.read(buffer) )!=-1 ) {//bis.read(buffer) 把讀到的資料放到buffer System.out.println("length="+length); bos.write(buffer,0,length);//write(byte[] b, int off, int len) } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ``` ## 總結: 讀檔案文字: FileInputStream fis = new FileInputStream("C:\\java\\Employees.csv"); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); 搭配 readLine();//讀取檔案String文字 寫檔案文字: FileOutputStream fis = new FileOutputStream("C:\\java\\char_1.txt"); OutputStreamWriter osw = new OutputStreamWriter(fis, "編碼類型");//UFF8或是MS950 BufferedWriter bw = new BufferedWriter(osw); 搭配 write();//寫String進檔案 ## 補充 Scanner 輸入文字: 語法; Scanner sc = new Scanner(System.in); #### 方法 1. nextInt();//取得使用者輸入之文字