前言

Java文件读写能够分为字节流和字符流,之前始终没有好好演绎以下,借此机会做一个小笔记。

目录

字节流

字节流就是一个字节一个字节的传输,最罕用的就是FileInputStream和FileOutputStream,代码如下

 public static void  readByFIS(String filepath) {        File file = new File(filepath);        try {            FileInputStream fis = new FileInputStream(file);            byte [] bytes = new byte[fis.available()];            while (fis.read(bytes, 0, bytes.length) != -1) {                System.out.println(new String(bytes));            }            fis.close();        } catch (IOException e) {            e.printStackTrace();        }    }    public static void writeByFIS(String filepath) {        File file = new File(filepath);        if(!file.exists()) {            try {                file.createNewFile();            } catch (IOException e) {                e.printStackTrace();            }        }        try {            FileOutputStream fos = new FileOutputStream(file, true);            fos.write("从此无可爱良夜,任他明月下西楼\n".getBytes());            fos.close();        } catch (IOException e) {            e.printStackTrace();        }    }

留神在读的的时候咱们使用了available去获取一个和文件大小刚好的空间,这存在一个问题,如果文件过大可能会导致内存溢出。

应用BufferedInputStream 和BufferedOutputStream如下

public static void readByBIS(String filepath) {        File file = new File(filepath);        if(file.exists()) {            try {                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));                byte[] bytes = new byte[bis.available()];                bis.read(bytes);                System.out.println(new String(bytes));                bis.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }    public static void writeByBIS(String filepath) {        File file = new File(filepath);        if(!file.exists()) {            try {                file.createNewFile();            } catch (IOException e) {                e.printStackTrace();            }        }        try {          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filepath));            bos.write("从此无可爱良夜,任他明月下西楼".getBytes());            bos.flush();            bos.close();        } catch (IOException e) {            e.printStackTrace();        }    }

字符流

使用FileWriter和FileReader

public static void writeByFR(String filepath) {        File file = new File(filepath);        if(!file.exists()) {            try {                file.createNewFile();            } catch (IOException e) {                e.printStackTrace();            }        }        try {            FileWriter writer = new FileWriter(filepath, true);            writer.write("从此无可爱良夜,任他明月下西楼");            writer.close();        } catch (IOException e) {            e.printStackTrace();        }    }    public static void readByFR(String filepath) {        File file = new File(filepath);        if(file.exists()) {            try {                FileReader reader = new FileReader(file);                char[] chars = new char[100];                reader.read(chars);                System.out.println(chars);                reader.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }

使用BufferedWriter和BufferedReader

ublic static void readByBR(String filepath) {        File file = new File(filepath);        String line;        try {            BufferedReader reader = new BufferedReader(new FileReader(file));            while ((line =  reader.readLine()) != null) {                System.out.println(line);;            }            reader.close();        } catch (IOException e) {            e.printStackTrace();        }    }    public static void writeByBR(String filepath) {        File file = new File(filepath);        if(!file.exists()) {            try {                file.createNewFile();            } catch (IOException e) {                e.printStackTrace();            }        }        try {            BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));            writer.write("从此无可爱良夜,任他明月下西楼");            writer.flush();            writer.newLine();            writer.close();        } catch (IOException e) {            e.printStackTrace();        }    }

最初

点赞就是最大的反对,更多信息关注公众号QStack,追寻最纯正的技术,享受编程的高兴。