2013年12月3日

JAVA I/O處理

 I/O處理

我們可以使用Input/Output來讀取外部的檔案/資料(檔案、console、網路資料),java.io類別庫提供很多函式功能,提供程式設計師方便使用。在JAVA SE7 會新增java.nio主要是增加io新的函式功能。

  • IO Stream 
IO分為兩種接收資料與傳送資料的模式,

(1) 位元模式(Byte Stream)
位元模式下就是輸入或是輸出資料須均要以8bit的位元,要完成一個位元模組可以利用InputStream或是OutputStream來完成。

Name: 文字檔案複製
Desc : 將文字檔複製到另一個檔案
  FileInputStream fis = null ;
OutputStream fos = null ;
try{
fis = new FileInputStream("input.txt");
fos = new FileOutputStream("output.txt") ;
  int c ;
  while((c = fis.read())!= -1){
  fos.write(c);
  }
}finally{
  if(fis!=null){
    fis.close();
  }
  if(fos!=null){
    fos.close();
}
}

(2) 字元模式(Character Streams)
字元模式主要是利用Unicode來做傳輸,當我們利用Character Stream IO做傳送時,他會自動轉成為local character的文字集,大多的應用程式都是字元模式

Name: 文字檔案複製
Desc : 將文字檔複製到另一個檔案
  FileReader rd = null ;
FileWriter rw =null ;

try{
//宣告實體
int c ;
rd = new FileReader("input.txt") ;
  rw = new FileWriter("output.txt") ;
  while((c=rd.read())!= -1){
  rw.write(c);
  }
}catch(IOException ioe){

}finally{
  if(rd!=null){
  rd.close();
}
if(rw !=null ){
rw.close();
}
}

   在常見的狀況下,我們會讀取大的資料比較常會遇到,所以IO提供以行為單位的方式來存取,我們利用緩衝的IO來實做BufferedReader,我們利用BufferedReaer的功能來實做一個簡單資料庫的功能。

Name: 簡單資料庫
Desc : 利用文字檔建立資料庫,來存取資料
 public class SimpleDataBase {
private static Map<String, String> map = new HashMap<String, String>() ;
public static void main(String[] args) throws IOException {

BufferedReader breader = null ;
BufferedWriter bwriter = null ;

try{
  breader = new BufferedReader(new FileReader("env_table.txt")) ;
  String line = "" ;
  while((line = breader.readLine())!= null){
  String value ;
  String key ;
  int index ;
  index = line.indexOf("=") ;
  key = line.substring(0, index).trim() ;
  value = line.substring(index+1, line.length()).trim() ;
  setMap(key, value);
}

System.out.println("================");
System.out.println("Color:"+getMap("color"));

}catch (Exception e) {
  System.err.println(e.toString());
}finally{
  if(breader != null){
  breader.close();
  }
  if(bwriter != null){
  bwriter.close();
  }
}
}

private static void setMap(String key, String value){
map.put(key, value);
}

private static String getMap(String key){
return map.get(key).toString();
}
}

  • BufferedReader和BufferedWriter
利用FileReader或是FileInputStream 都是直接叫OS逐一字元的讀取外部IO的資料,這種方式會造成OS系統的繁忙使效能不佳,通常會利用會先將外部資料存放在記憶體中,也稱為緩衝區(buffered IO)。
   1. BufferedReader breader = new BufferedReader(new FileReader("reader.txt"))
   2. BuffertedWriteer bwriter = new BufferedWriter(new FileWriter("writer.txt"))
  • Data Stream
資料串流是以二進位檔讀取與寫入資料,基本資料型別(int, double, float, boolean...)和字串,需要將資料以二進制排好既可,DataInputStream和DataOutputStream物件都可以支援資料型態的轉換。


    Name: BinaryDatabase資料庫
    Desc : 有一份早餐清單可以提供客人去選取,內容名稱、價錢,但是可以隨意的更動名單內容, 
              利用二元進制檔來當作早餐清單內容。

      早餐清單
    id    item      price 
     1     milk       50.0
     2     water     10.0
     3     ham       5.0
     4     egg        8.0
    public class TestDataStream {

    public class Breakfast{
    private int id ;
    private String item ;
    private double price ;

    public Breakfast(int id, String item, double price){
    this.id = id ;
    this.item = item ;
    this.price = price ;
    }
    }

    public static void main(String[] args) throws IOException {

      TestDataStream tds = new TestDataStream() ;
      //定義早餐菜單
      List<Breakfast> breakfastList = new ArrayList<>() ;
      breakfastList.add(  tds.new Breakfast( 1, "milk", 50.0));
      breakfastList.add(  tds.new Breakfast( 2, "water", 10.0));
      breakfastList.add(  tds.new Breakfast( 3, "ham", 5.0));

        //寫入早餐明細
        DataInputStream dis =null ;
        DataOutputStream dos = null ;

    try{
     System.out.println("寫入早餐明細");
     dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("breakfast.txt"))) ;

     for(int i=0 ; i< breakfastList.size() ;i++){
         Breakfast b = breakfastList.get(i) ;
        dos.writeInt(b.id);
        dos.writeUTF(b.item);
        dos.writeDouble(b.price);
     }

     //關閉文字檔
     dos.close();
     System.out.println("讀取資料");
      dis = new DataInputStream(new BufferedInputStream(new FileInputStream("breakfast.txt"))) ;
      System.out.format("%10s %10s %10s \n", "序號", "型號","價格");
      while(true){
      Breakfast b = tds.new Breakfast(dis.readInt(), dis.readUTF(), dis.readDouble()) ;
      System.out.format("%10d %10s %10f  \n", b.id, b.item , b.price );
      }
    }catch(IOException ioe){
        System.out.println(ioe.toString());
        ioe.printStackTrace();
    }finally{
      if(dos!=null){
      dos.close();
    }
        if(dos!=null){
        dis.close();
    }
    }
    }
    }


    Hot : 如果價格需要更高的精準度,需要定義BigDecimal的物件是否可以使用?
    • Object Streams
    主要的類別:ObjectOutputStream, ObjectInputStream
    主要支援物件的資料型態(Double、Bigdecimal...),但是要使用Object需要實做Serializable.

    NIO 

    現在我們討論一下何謂檔案系統,通常我們在儲存檔案時,是以階層是去存放檔案,那JAVA程式如何去知道目前的目錄的所在位置 ?
    • 了解檔案目錄結構
     每一個檔案系統都會有一個根目錄(root),在win系統下可能是C:\,linux系統下則為/ ,所以當要表達a.mp3的位置是" /音樂/a.mp3",但是如果子目錄太深每次這樣表示變成一件很繁瑣的事情,那要如何去解決這個問題? 答案: 相對路徑(Relative directory)



    • 相對路徑與絕對路徑(Relative or Absolute)
    絕對路徑就是都會以root目錄表示,
    相對路徑則會以目前的位置去表示該檔案的路徑,例如; 目前在音樂中,表達底下的音樂檔為
    a.mp3,要表達重要目錄下的檔案為"../重要/2013/01.txt"

    • 連結符號(Symbolic Links)
    在windows系統下有當你要進入快速其他資料夾,可以建立捷徑,表示它只是個連結並不有實際檔案的功能,只是方便進入其他目錄,這種連結符號在SE 7開始支援屬性的設定。

    • Path處理
    類別: Path
    Path類別使用在讀取目錄、目錄權限、轉換路徑等等的功能實現

    常用的功能 例子
    建立目錄或是檔案 Path p1 = Paths.get("/音樂/");
    Path p2 = Paths.get("/音樂/a.mp3");
    Path p3 = Paths.get(URI.create("file:///音樂"));

    取的目錄的相關資訊 Path path = Paths.get("C:\\音樂\\a.mp3");
    System.out.format("toString: %s%n", path.toString());
     System.out.format("getFileName: %s%n", path.getFileName()); System.out.format("getName(0): %s%n", path.getName(0)); System.out.format("getNameCount: %d%n", path.getNameCount()); System.out.format("subpath(0,2): %s%n", path.subpath(0,2)); System.out.format("getParent: %s%n", path.getParent()); System.out.format("getRoot: %s%n", path.getRoot());
    轉換網路目錄 Path p1 = Paths.get("C:\\Users");
     System.out.format("%s%n", p1.toUri());
    轉換為實體目錄 Path p1 = Paths.get("C:\\Users");
     System.out.println(p1.toRealPath());
    結合兩個目錄 Path p1 = Paths.get("C:\\Users");
    Path p2 = Paths.get("C:\\Users\\bryan");
    System.out.println(p1.resolve(p2));
    System.out.println(p1.resolve("Account"));
    結果:
    C:\Users\bryan
    C:\Users\Account
    Note:
    // Solaris syntax
          Path path = Paths.get("sally/bar");
     // Microsoft Windows syntax
         Path path = Paths.get("sally\\bar");

    *Glob
    *  符號表任何字串或是數字
    **  
    ? 任一字元
    {item1, item2, item3} 表示為多個字串
    [ ]  表示為一個區段,Ex: [0-9] 為0-9之間
      

    • File 檔案資訊
    File類別是用來讀取或是設定檔案資訊、目錄資訊等等,

    建構子

    Name Description
    File(File paraent, String fileName) 設定檔案名稱
    File(String pathname/path) 建立一個File實例,參數可以是檔案名稱或是路徑
    File(String path, String fileName) 第一個參數: 路徑字串 ,第二個參數:檔案字串 


    函式

    Name Description
    boolean createNewFile() 建立新檔
    static File createTempFile(String prefix, String suffix) 建立暫時檔
    static File createTempFile( String prefix, String suffix, File directory) 建立暫存檔,prefix: 檔名 , suffix:副檔名;directory:目錄
    boolean mkdir() 建立目錄
    boolean mkdirs() 建立目錄                      
    boolean exists() 檔案是否存在
    boolean renameTo( File file) 更改檔案名稱
    boolean delete() 刪除檔案
    boolean isAbsolute() 是否絕對路徑
    boolean isDirectory() 是否為目錄
    boolean isFile() 是否為檔案
    boolean isHidden() 是否為隱藏檔
    boolean isRead() 是否可讀
    boolean isWrite() 是否可寫
    String getName() 取得檔案名稱
    String  getAbsoluteFile() 取得檔案絕對路徑
    String  getAbsolutePath() 取得檔案絕對路徑
    String getPath() 取得路徑
    String getParent() 取得上層路徑
    Long length() 檔案大小
    String[] list() 回傳所有的目錄下的檔案或是目錄
    File listFiles() 回傳所有的目錄下的檔案
    long lastModified() 回傳最後修改時間