2017.8.16File和IO流的小案列

Io流之遞歸法:
什麼是遞歸:方法定義中調用方法本身的現象
注意事項:
* A:遞歸一定要有出口,否則就是死遞歸
* B:遞歸的次數不能太多,否則就內存溢出
* C:構造方法不能遞歸使用


有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問第二十個月的兔子對數爲多少?


方法一:

public static int fib(int n) {
        if (n == 1 || n == 2) {
            return 1;
        } else {
            return fib(n - 1) + fib(n - 2);
        }
    }

方法二:

int a = 1;
        int b = 1;
        for (int x = 0; x < 18; x++) {
            // 臨時變量存儲上一次的a
            int temp = a;
            a = b;
            b = temp + b;
        }
        System.out.println(b);

方法三


int[] arr = new int[20];
        arr[0] = 1;
        arr[1] = 1;
        // arr[2] = arr[0] + arr[1];
        // arr[3] = arr[1] + arr[2];
        // ...
        for (int x = 2; x < arr.length; x++) {
            arr[x] = arr[x - 2] + arr[x - 1];
        }
        System.out.println(arr[19]);

案列 二(獲取所有.Java的文件)(很重要可以試試看)


public static void main(String[] args) {
        // TODO Auto-generated method stub

        File file=new File("F:\\");
        getAlljavaFilePath(file);
    }

    private static void getAlljavaFilePath(File file) {
        // TODO Auto-generated method stub
         File[] list= file.listFiles();
            for(File as:list){
                if(as.isDirectory()){
                    getAlljavaFilePath(as);
                }else{
                    if(as.getName().endsWith(".java")){
                        System.out.println(as.getAbsolutePath());
                    }

                }
            }
    }

案列三:刪除文件


public static void main(String[] args) {
        // 封裝目錄
        File srcFolder = new File("demo");
        // 遞歸實現
        deleteFolder(srcFolder);
    }

    private static void deleteFolder(File srcFolder) {
        // 獲取該目錄下的所有文件或者文件夾的File數組
        File[] fileArray = srcFolder.listFiles();

        if (fileArray != null) {
            // 遍歷該File數組,得到每一個File對象
            for (File file : fileArray) {
                // 判斷該File對象是否是文件夾
                if (file.isDirectory()) {
                    deleteFolder(file);
                } else {
                    System.out.println(file.getName() + "---" + file.delete());
                }
            }

            System.out
                    .println(srcFolder.getName() + "---" + srcFolder.delete());
        }
    }

IO流的學習


1.用來上傳和下載文件等操作。

2.IO流用來設備之間的傳輸問題。


    IO流的分類:
 *      流向:
 *          輸入流 讀取數據
 *          輸出流 寫出數據
 *      數據類型:
 *          字節流
 *              字節輸入流   讀取數據    InputStream
 *              字節輸出流   寫出數據    OutputStream
 *          字符流
 *              字符輸入流   讀取數據    Reader
 *              字符輸出流   寫出數據    Writer
 *          注意:一般我們在探討IO流的時候,如果沒有明確說明按哪種分類來說,默認情況下是按照數據類型來分的。   

案列一:

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("QST.txt");
        fos.write("hello,IO".getBytes());
        fos.write("java".getBytes());
    }
}

字節輸出流操作步驟:

A:創建字節輸出流對象

B:調用write()方法

C:釋放資源


案列二:


public class FileOutputStreamDemo2 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("yan55.txt");
        byte[] bys={97,98,99,100,101};
        fos.write(bys);
        fos.write(bys,1,3);

        //釋放資源
        fos.close();
    }
}

讀取數據的方式:
* A:int read():一次讀取一個字節
* B:int read(byte[] b):一次讀取一個字節數組
*


案列三:

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

        FileInputStream fis = new FileInputStream("haah.txt");

        int by = 0;
        while ((by = fis.read()) != -1) {
            System.out.print((char) by);
        }
        // 釋放資源
        fis.close();
    }
}

複製文本文件。

  • 數據源:從哪裏來
    a.txt – 讀取數據 – FileInputStream

    目的地:到哪裏去
    b.txt – 寫數據 – FileOutputStream


案列四:

public class CopyFileDemo {
    public static void main(String[] args) throws IOException {
        // 封裝數據源
        FileInputStream fis = new FileInputStream("a.txt");
        // 封裝目的地
        FileOutputStream fos = new FileOutputStream("b.txt");

        int by = 0;
        while ((by = fis.read()) != -1) {
            fos.write(by);
        }

        // 釋放資源(先關誰都行)
        fos.close();
        fis.close();
    }
}

案列五:

public class CopyImageDemo {
    public static void main(String[] args) throws IOException {
        // 封裝數據源
        FileInputStream fis = new FileInputStream("e:\\haha.jpg");
        // 封裝目的地
        FileOutputStream fos = new FileOutputStream("mn.jpg");

        // 複製數據
        int by = 0;
        while ((by = fis.read()) != -1) {
            fos.write(by);
        }

        // 釋放資源
        fos.close();
        fis.close();
    }
}

案列六:

public class CopyFileDemo {
    public static void main(String[] args) throws IOException {
        // 封裝數據源
        FileInputStream fis = new FileInputStream("c:\\a.txt");
        FileOutputStream fos = new FileOutputStream("d:\\b.txt");

        // 複製數據
        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = fis.read(bys)) != -1) {
            fos.write(bys, 0, len);
        }

        // 釋放資源
        fos.close();
        fis.close();
    }
}

案列七:

public class BufferedInputStreamDemo {
    public static void main(String[] args) throws IOException {
        // BufferedInputStream(InputStream in)
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                "bos.txt"));

        // 讀取數據
        // int by = 0;
        // while ((by = bis.read()) != -1) {
        // System.out.print((char) by);
        // }
        // System.out.println("---------");

        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = bis.read(bys)) != -1) {
            System.out.print(new String(bys, 0, len));
        }

        // 釋放資源
        bis.close();
    }
}

案列八:

public class BufferedOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        // BufferedOutputStream(OutputStream out)
        // FileOutputStream fos = new FileOutputStream("bos.txt");
        // BufferedOutputStream bos = new BufferedOutputStream(fos);
        // 簡單寫法
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("bos.txt"));

        // 寫數據
        bos.write("hello".getBytes());

        // 釋放資源
        bos.close();
    }
}

案列九:

public class CopyMp4Demo {
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        // method1("e:\\haah.mp4", "copy1.mp4");
        // method2("e:\\hehe.mp4", "copy2.mp4");
        // method3("e:\haha.mp4", "copy3.mp4");
        method4("e:\\haha.mp4", "copy4.mp4");
        long end = System.currentTimeMillis();
        System.out.println("共耗時:" + (end - start) + "毫秒");
    }

    // 高效字節流一次讀寫一個字節數組:
    public static void method4(String srcString, String destString)
            throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                srcString));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destString));

        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = bis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }

        bos.close();
        bis.close();
    }

    // 高效字節流一次讀寫一個字節:
    public static void method3(String srcString, String destString)
            throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                srcString));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destString));

        int by = 0;
        while ((by = bis.read()) != -1) {
            bos.write(by);

        }

        bos.close();
        bis.close();
    }

    // 基本字節流一次讀寫一個字節數組
    public static void method2(String srcString, String destString)
            throws IOException {
        FileInputStream fis = new FileInputStream(srcString);
        FileOutputStream fos = new FileOutputStream(destString);

        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = fis.read(bys)) != -1) {
            fos.write(bys, 0, len);
        }

        fos.close();
        fis.close();
    }

    // 基本字節流一次讀寫一個字節
    public static void method1(String srcString, String destString)
            throws IOException {
        FileInputStream fis = new FileInputStream(srcString);
        FileOutputStream fos = new FileOutputStream(destString);

        int by = 0;
        while ((by = fis.read()) != -1) {
            fos.write(by);
        }

        fos.close();
        fis.close();
    }

----------
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章