无声specialweek

从零开始学Java-Day13

字节流

字节输入流 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();
        }
    }

}

posted on 2021-06-17 18:43  无声specialweek  阅读(25)  评论(0编辑  收藏  举报

导航