2008年8月9日 星期六

Java - I/O - 1

1.藉由I/O可以在程式執行過程中存取外部資料(如檔案、Socket、Console、記憶體等等)
2.File Class 不是一個標準的I/O類別,在java.io類別函式中也是唯一的non-stream class。不能讀取檔案、不能寫入檔案。
3.File Class 用於收集檔案(目錄)相關資訊,如建立檔案(並非寫入檔案內容)、修改檔名、設定/讀取檔案屬性、測試檢查檔案是否存在、列出檔案目錄等等

以下為 File 的部份操作實例
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package io;

import java.io.*;

/**
 *
 * @author Administrator
 */
public class NewClass {
    private static String path = "C:/haoxiao/io/";
    
    public static void main(String[] args)throws IOException {
        
        //建立目錄,並判斷目錄是否存在
        System.out.println("================建立目錄,並判斷目錄是否存在================");
        File mkdir = new File(path);
        if(mkdir.mkdirs()) {
            System.out.println("目錄 C:/haoxiao/io/ 建立完成");
        } else {
            System.out.println("目錄 C:/haoxiao/io/ 已存在");
        }
        
        //建立檔案
        System.out.println("================建立檔案================");
        File file = new File(path , "Test.txt");
        if(!file.exists()) {
            System.out.println("檔案尚未建立...");
            if(file.createNewFile()) {
                System.out.println("檔案建立成功");
            } else {
                System.out.println("檔案建立失敗");
            }
        } else {
            System.out.println("檔案已經建立");
        }
        
        //更改檔名及路徑
        System.out.println("================更改檔名及路徑================");
        if(!file.exists()) {
            System.out.println("檔案不存在");
        }else{
            System.out.println("檔案更名中...");
            if(!new File(path+"changName.txt").exists()) {
                if(file.renameTo(new File(path + "changName.txt"))) {
                    System.out.println("檔案更名成功");
                } else {
                    System.out.println("檔案更名失敗");
                }
            } else {
                System.out.println("檔案已存在");
            }
        }

        showFile(path);
        
        //刪除檔案
        System.out.println("================刪除檔案================");
        if(file.exists()) {
           file.delete(); 
        }
        showFile(path);
        
        System.out.println("================實做Linux ls指令================");
        path = "C:/windows";
        File file1 = new File(path);
        showDetailFile(file1);
        
    }
    
    public static void showFile(String path) {
        for(String filename : new File(path).list()) {
            System.out.println("Filename : " + filename);
        }
    }
    
    public static void showDetailFile(File file) {
        String fileStrings[] = file.list();
        for(String fileString : fileStrings) {
            File file1 = new File(file.getAbsoluteFile() + File.separator + fileString);
            String isDir = isDirectory(file1);
            String isHidden = isHidden(file1);
            String canRead = canRead(file1);
            String canWrite = canWrite(file1);
            String canExecute = canExecute(file1);
            System.out.println(isDir + "\t" +canRead+canWrite+canExecute + "\t" + isHidden+ fileString + "\t " + file1.lastModified());
        }
    }
    
    public static String canRead(File file) {
        if(file.canRead()) {
            return "r";
        } else {
            return "_";
        }
    }
    
    public static String canWrite(File file) {
        if(file.canWrite()) {
           return "w";
        } else {
            return "_";
        }
    }
    
    public static String canExecute(File file) {
        if(file.canExecute()) {
            return "x";
        } else {
            return "_";
        }
    }
    
    public static String isHidden(File file) {
        if(file.isFile()) {
            return "";
        } else {
            return ".";
        }
    }
    
    public static String isDirectory(File file) {
        if(file.isDirectory()) {
            return "d";
        } else {
            return "_";
        }
    }
}

0 意見: