1. 程式人生 > 其它 >從零開始學Java-Day12

從零開始學Java-Day12

位元組流

位元組輸入流 InputStream
  • public abstract class **InputStream**
  • 此抽象類是表示位元組輸入流的所有類的超類。

FileIputStream操作檔案的位元組輸入流 - 上述的子類

read():每次滴哦用都會讀取一個位元組,如果讀取到末尾,返回-1

package cn.tedu.file;

import java.io.*;

//此類用於測試位元組輸入流
public class TestIn {
    public static void main(String[] args) throws IOException {
       // method();
        method2();
    }
    //高效讀取
    private static void method2() throws IOException {
        InputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream("d:\\x\\1.txt"));
            byte[] bs = new byte[10];
            int len = -1;
            while ((len = in.read(bs)) != -1){
                System.out.println(new String(bs, 0, len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            assert in != null;
            in.close();
        }
    }

    private static void method() throws IOException {
        InputStream in = null;
        try {
            in = new FileInputStream(new File("d:\\x\\1.txt"));
            /*read()每次讀一個字元,當讀到末尾時輸出-1表示已經讀完*/
            int len = -1;
            /*定義len時因為每次執行read()都會往下讀取一個數,無法列印當前
            的數*/
            while ((len = in.read()) != -1){
                System.out.println(len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            assert in != null;
            in.close();
        }
    }
}
位元組輸出流 OnputStream
  • public abstract class **OutputStream**
  • 此抽象類是表示位元組輸出流的所有類的超類。

FileOutputStream操作檔案的位元組輸出流 - 上述的子類

write():每次都用都會輸出一個位元組

FileReader 檔案字元輸入流

  • FileReader(File file)
  • FileReader(String pathName)

BufferedReader 高效緩衝字元輸入流

  • BufferedReader(Reader in)一般使用時傳入Reader的子類
package cn.tedu.file;

import java.io.*;

//本類用於測試位元組輸出流
public class TestOut {
    public static void main(String[] args) throws IOException {
        //method();
        method2();
    }

    private static void method2() throws IOException {
        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream("d:\\x\\2.txt", true));
            out.write("哈哈哈哈".getBytes());
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            assert out != null;
            out.close();
        }
    }

    private static void method() throws IOException {
        OutputStream out = null;
        try {
            out = new FileOutputStream("d:\\x\\1.txt");
            out.write("abcdefghijklmn".getBytes());

        }catch (IOException e){
            e.printStackTrace();
        }finally {
            assert out != null;
            out.close();
        }
    }
}

字元流

字元輸入流Reader

抽象,需要利用多型來建立物件

package cn.tedu.file;

import java.io.*;

//本類用於測試字元流的讀取
public class TestIn2 {
    public static void main(String[] args) throws IOException {
        method();
        method2();
    }

    private static void method2() throws IOException {
        BufferedReader read = null;
        try {
            read =  new BufferedReader(new FileReader("d:\\x\\1.txt"));
            String str;
            while ((str = read.readLine()) != null){
                System.out.println(str);
            }
        }catch (IOException e){
            e.printStackTrace();
        } finally {
            assert read != null;
            read.close();
        }

    }

    private static void method() throws IOException {
        Reader read = null;
        try {
            read = new FileReader("d:\\x\\1.txt");
            char[] cs = new char[10];
            int len = -1;
            while ((len = read.read(cs)) != -1){
                System.out.println(new String(cs, 0, len));
            }
        }catch (IOException e){
            e.fillInStackTrace();
        }finally {
            assert read != null;
            read.close();
        }

    }
}
字元輸出流Writer

抽象,需要利用多型來建立物件

package cn.tedu.file;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

//此類用於測試字元輸出
public class TestOut2 {
    public static void main(String[] args) throws IOException {
        //method();
        method2();
    }

    private static void method2() throws IOException {
        Writer writer = null;
        try {
            writer = new BufferedWriter(new FileWriter("d:\\x\\1.txt",true));
            writer.append("123");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            assert writer != null;
            writer.close();
        }
    }

    private static void method() throws IOException {
        Writer writer = null;
        try {
            writer = new FileWriter("d:\\x\\1.txt",true);
            writer.write("wawaaw");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            assert writer != null;
            writer.close();
        }
    }
}

拓展練習

package cn.tedu.file;

import java.io.*;
import java.util.Scanner;

//本類用於拓展練習檔案複製案例
public class TestCopyFile {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入要複製檔案的絕對路徑:");
        String file = scanner.next();
        System.out.println("請輸入檔案複製位置的絕對路徑:");
        String toFile = scanner.next();
        File f = new File(file);
        File t = new File(toFile);
        //copyFile(f,t);
        copyPicture(f,t);
        scanner.close();
    }

    private static void copyPicture(File f, File t) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new  BufferedInputStream(new FileInputStream(f));
            out = new BufferedOutputStream(new FileOutputStream(t));
            int len = -1;
            while ((len = in.read()) != -1){
                out.write(len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            assert out != null;
            out.close();
            in.close();
        }
    }

    private static void copyFile(File f, File t) throws IOException {
        Reader reader = null;
        Writer writer = null;
        try {
            reader = new BufferedReader(new FileReader(f));
            writer = new BufferedWriter(new FileWriter(t,true));
            int len = -1;
            while ((len = reader.read()) != -1){
                writer.write(len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            assert writer != null;
            writer.close();
            assert reader != null;
            reader.close();
        }
    }

}