檔案I/O常用的套件
java.io.File | 檔案本體及相關資訊 |
java.io.FileInputStream | 以byte為單位讀取2進制的內容 |
Java.io.FileOutputStream | 以byte為單位輸出2進制的內容 |
java.io.FileRead | 讀取文字內容 |
java.io.FileWriter | 輸出文字內容 |
建立檔案
使用java.io.File的createNewFile方法建立檔案。1 2 3 4 5 6 7 8 9 10 11 12 13 | File file = new File( "c:/sample.txt" ); System.out.println( "建立 " + file.getPath()); try { //建立新檔 if (file.createNewFile()) { System.out.println( "新檔案建立成功" ); } else { System.out.println( "新檔案建立失敗" ); } } catch (IOException e) { } |
建立目錄
使用java.io.File的mkdir方法建立目前。1 2 3 4 5 6 7 8 9 10 | File file1 = new File( "c:/dirsample" ); System.out.println( "建立" + file1.getPath()); //建立新目錄 if (file1.mkdir()) { System.out.println( "建立目錄成功" ); } else { System.out.println( "建立目錄失敗" ); } |
檢查檔案或目錄是否存在
使用java.io.File的exists方法判斷檔案是否存在。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | File file1 = new File( "c:/sample.txt" ); //檢查檔案是否存在 if (file1.exists()) { System.out.println( "sample.txt存在" ); } else { System.out.println( "sample.txt不存在" ); } File file2 = new File( "xxxx.txt" ); //檢查檔案是否存在 if (file2.exists()) { System.out.println( "xxxx.txt存在" ); } else { System.out.println( "xxxx.txt不存在" ); } |
取得檔案路徑
使用java.io.File的getName, getParent, getPath方法取得檔的路徑資訊。1 2 3 4 | File file1 = new File( "c:/sample.txt" ); System.out.println( "檔名 : " + file1.getName()); System.out.println( "所在目錄: " + file1.getParent()); System.out.println( "完整路徑: " + file1.getPath()); |
取得目錄中檔案列表
使用java.io.File的list方法取得目錄中的所有檔案。1 2 3 4 5 6 7 8 | File file1 = new File( "c:/dirsample" ); //取得指定目錄中所有的檔案、目錄的名稱 String list1[] = file1.list(); for ( int i= 0 ;i<list1.length;i++) { System.out.println((i + 1 ) + ": " + list1[i]); } |
取得檔案大小
使用java.io.File的length方法取得檔案大小。1 2 3 4 5 6 | File file1 = new File( "C:/sample.txt" ); System.out.print(file1.getPath() + "的長度是" ); //取得檔案大小 System.out.println(file1.length() + "位元組" ); |
判斷檔案是否可以存取
使用java.io.File的canRead, canWrite方法得知檔案是否唯讀。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | File file1 = new File( "c:/sample.txt" ); //判斷是否可以讀取 if (file1.canRead()) { System.out.println(file1.getName() + "可以讀取" ); } else { System.out.println(file1.getName() + "不可以讀取" ); } //判斷是否可以寫入 if (file1.canWrite()) { System.out.println(file1.getName() + "可以寫入" ); } else { System.out.println(file1.getName() + "不可以寫入" ); } |
取得最後修改時間
使用java.io.File的lastModified方法得知檔案最後修改時間。1 2 3 4 5 6 7 8 9 10 11 12 | File file1 = new File( "c:/sample.txt" ); //顯示時不改變顯示格式 System.out.print(file1.getPath() + " 最後一次修改的時間是 " ); System.out.println(file1.lastModified()); //套用格式,顯示檔案最後一次修改的時刻 Date date1 = new Date(file1.lastModified()); DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); System.out.print(file1.getPath() + " 最後一次修改的時間是 " ); System.out.println(df.format(date1)); |
建立暫存檔
使用java.io.File的createTempFile方法建立暫存檔。
createTempFile(String prefix, String suffix, File directory)有3個建構子
String prefix: 檔名,至少3個字元
String suffix: 副檔名,不指定(null)的情況下,預設副檔名為.tmp
File directory: 資料夾路徑,不指定(null)的情況下,預設為作業系統的預設暫存檔路徑如:c:/tmp
1 2 3 4 5 6 7 8 9 10 11 12 13 | try { File file1 = new File( "C:/tmp" ); //建立暫存檔 File file2 = File.createTempFile( "sample" , "java" ,file1); if (file2.exists()) { System.out.println( "建立暫存檔 " + file2.getPath()); } else { System.out.println( "無法建立暫存檔" ); } } catch (IOException e) { } |
自動刪除檔案
使用java.io.File的deleteOnExit方法在程式結束時動刪除檔案或目錄。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.io.*; class mThread extends Thread { public void run() { try { System.out.println( "在執行緒中停止10秒" ); System.out.println( "請在這段時間內確認暫存檔已經建立了" ); sleep( 10000 ); } catch (InterruptedException e) { } } } class Main{ public static void main(String args[]) { try { File file1 = new File( "C:/" ); File file2 = File.createTempFile( "temp" , "java" ,file1); //程式結束時自動刪除檔案 file2.deleteOnExit(); if (file2.exists()) { System.out.println( "已建立" + file2.getPath()); } else { System.out.println( "無法建立暫存檔案" ); } } catch (IOException e) { } //啟動執行緒延遲10秒 mThread thread = new mThread(); thread.start(); } } |
更改檔名
使用java.io.File的renameTo方法更改檔名。1 2 3 4 5 6 7 8 9 10 11 12 | File file1 = new File( "c:/sample.txt" ); File file2 = new File( "c:/newsample.txt" ); System.out.print( "將 " +file1.getPath() + " 的檔名改為 " ); System.out.println(file2.getPath()); //更改檔名 if (file1.renameTo(file2)) { System.out.println( "更改成功" ); } else { System.out.println( "更改失敗" ); } |
刪除檔案或目錄
使用java.io.File的delete方法刪除檔案。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | File file1 = new File( "c:/sample.txt" ); System.out.println( "刪除 " + file1.getPath() + " 檔案" ); //刪除檔案 if (file1.delete()) { System.out.println( "刪除檔案成功" ); } else { System.out.println( "刪除檔案失敗" ); } File file2 = new File( "dirsample" ); System.out.println( "刪除 " + file2.getPath() + " 目錄" ); //刪除目錄 if (file2.delete()) { System.out.println( "刪除目錄成功" ); } else { System.out.println( "刪除目錄失敗" ); } |
讀取文字檔
使用java.io.FileReader可以從檔案讀取內容。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | System.out.println( "開始讀取資料" ); try { char data[] = new char [ 100 ]; //開啟檔案 FileReader fr = new FileReader( "c:/sample.txt" ); //讀取資料 int charscount = fr.read(data); String str = new String(data, 0 ,charscount); System.out.println(str); //關閉檔案 fr.close(); } catch (Exception e) { //例外處理 System.out.println( "發生了" + e + "例外" ); } System.out.println( "資料讀取結束" ); |
寫入文字檔
使用java.io.FileWrite可以從檔案讀取內容。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | System.out.println( "開始將資料寫入檔案" ); try { //開啟檔案 FileWriter fw = new FileWriter( "c:/sample.txt" ); //將資料寫入檔案 fw.write( "123456789" ); //關閉檔案 fw.close(); } catch (Exception e) { //例外處理 System.out.println( "發生了" + e + "例外" ); } System.out.println( "檔案寫入結束" ); |
沒有留言:
張貼留言