1. 程式人生 > >Java IO學習總結(一)

Java IO學習總結(一)

file flush writer directory 創建 str java 資源 tab

一、File 類

Java中不管文件還是目錄都可以使用File類操作,File能新建、刪除、重命名文件和目錄,但是不能訪問文件內容本身,訪問文件內容需要使用輸入輸出流。

File類主要使用:

public class FileTest {
    public static void main(String[] args) throws IOException {
        File file = new File("test");
        System.out.println("文件名稱:" + file.getName());
        System.out.println(
"文件路徑:" + file.getAbsolutePath()); System.out.println("是否是文件:" + file.isFile()); System.out.println("是否是路徑:" + file.isDirectory()); System.out.println("文件是否存在:" + file.exists()); // 創建文件 file.createNewFile(); System.out.println("文件是否存在:" + file.exists());
// 創建目錄 System.out.println("創建目錄是否成功:" + file.mkdir()); // 文件重命名 file.renameTo(new File("test1")); System.out.println("文件名稱:" + file.getName()); String [] fileList = file.list(); for (String str : fileList) { System.out.println("文件名稱:" + str); } } }

二、IO流

1)流的分類

Java的輸入輸出流都是從程序運行所在內存的角度劃分的。

輸入流:只能從中讀取數據,不能寫入數據

輸出流:只能向其中寫入數據,不能讀取數據。

字節流:字節流操作的是8位字節,主要以InputStream和OutStream作為基類

字符流:字符流操作的是16位的字符,主要以Reader和Writer作為基類

節點流:可以向一個特定IO設備(如磁盤、網絡)讀寫數據,稱為節點流

處理流:對一個已存在的流進行連接和封裝,通過封裝後的流來實現數據讀寫功能

2)字節流和字符流

  • InputStream和Reader
    主要方法使用
    public class InputStreamTest {
        public static void main(String[] args) {
                // 創建字節輸入流
                FileInputStream fis = null;
            try {
                fis = new FileInputStream("d:\\test.txt");
                // 創建每次讀取字節數的數組
                byte[] byt = new byte[1024];
                // 保存實際讀取的字節數
                int hasRead = 0;
                while((hasRead = fis.read(byt)) != -1){
                    // 字節轉換成字符輸出
                    System.out.println(new String(byt,0,hasRead));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally
            {
                // 關閉IO流
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public class ReaderTest {
        public static void main(String[] args) {
            // 創建字符流
            FileReader fr = null;
            try {
                fr = new FileReader("d:\\test.properties");
                // 創建每次讀取字符數組
                char [] ch = new char[32];
                // 保存時間讀取字符數
                int hasRead = 0;
                while((hasRead = fr.read(ch)) != -1){
                    System.out.println(new String(ch,0,hasRead));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • OutputStream和Writer
    主要方法使用
    public class OutputStreamTest {
        public static void main(String[] args) {
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                // 創建輸入輸出流
                fis = new FileInputStream("d:\\test.txt");
                fos = new FileOutputStream("d:\\test1.txt");
                
                byte[] byt = new byte[1024];
                int read = 0;
                while((read = fis.read(byt)) != -1){
                    // 讀取多少就輸出
                    fos.write(byt, 0, read);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    fos.close();
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public class WriterTest {
        public static void main(String[] args) {
            FileWriter fw = null;
            try{
                fw = new FileWriter("d:\\test2.txt");
                fw.write("這是一段測試文字。\r\n");
                fw.write("換行繼續展示。\r\n");
                fw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
        }
    }
    註意點:在使用完IO時記得關閉輸出流,因為關閉流可以保證流的物理資源被回收,還可以將輸出流緩沖區的數據flush到物理節點(關閉流之前自動執行flush()方法),保證程序正常運行。

3)處理流

  使用處理流來包裝節點流,通過處理流來執行輸入輸出功能,讓節點流與底層I/O設備、文件交互。

  處理流和節點流區別:流的構造參數不是一個物理節點,而且是已經存在的流,就是處理流,節點流的構造參數都是以物理節點為構造參數的。 

public class PrintStreamTest {
    public static void main(String[] args){
        try {
            FileOutputStream fos = new FileOutputStream("d:\\test2.txt");
            PrintStream ps = new PrintStream(fos);
            
            ps.print("普通字符串");
            ps.println(new PrintStreamTest());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        
    }
}

Java IO學習總結(一)