java基础—IO流

 

 

 一、字符流

读取文件

FileReader与BufferedReader区别:

https://blog.csdn.net/meihuai7538/article/details/75807624/

https://www.cnblogs.com/cookie1026/p/9703879.html

https://blog.csdn.net/tovegar/article/details/83455377

 

 

 

 

 

 

注: 第四行如果为空,则输出空,如果只有三行第四行没有内容,则输出null

 

另一种通过循环读取文件内容的方法:

 

bufferreader如何节约内存:例如上面循环读取文件内容方法,bufferreader每次只读取一行赋值给变量line,变量line保存到内存里,那么内存里只存储一行的数据,因此数据过多的时候不会撑爆内存。

 

 

写入文件

public static void writeCharactorFile() throws Exception{
        File file = new File("D:\\write.txt");
        FileWriter fileWriter = new FileWriter(file);
        //缓冲写入流
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        //写一个字符串
        //  \n--》换行符
        String s = "hello \n lemon \n aaa bbb";
        //第一个参数:原始你要写的字符串  第二个参数:写入的位置  第三个参数:写的内容长度
        //方法重载
        //bufferedWriter.write(s,0,s.length());
        bufferedWriter.write(s);
        //流记得关闭,否则的话内容是写不进去
        bufferedWriter.close();

 

总结:
字符流
File文件对象-->FileReader读取 --> BufferReader缓冲读取(效率高)
File文件对象-->FileWriter写入 --> BufferWriter缓冲写入(效率高)

异常
1、编译型异常 --》需要我们手动去处理
(1)try...catch...代码块内部处理
(2)throws关键字 向外抛出异常
2、运行时异常 --> 数组越界、分母0

 

复制一个文件的内容到另一个文件中

import java.io.*;

public class CopyFile {
    public static void main(String[] args) throws Exception {
        String oriFilePath = "C:\\Users\\before.txt";
        String copyFilePath = "C:\\Users\\after.txt";
        copyFile(oriFilePath,copyFilePath);
    }

    public static void copyFile(String oriFilePath,String copyFilePath ) throws Exception {
        File oriFile = new File(oriFilePath);
        File copyFile = new File(copyFilePath);
        FileReader fileReader = new FileReader(oriFile);
        FileWriter fileWriter = new FileWriter(copyFile);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        String line;
        while ((line = bufferedReader.readLine()) != null){
            //写入每行内容,直接写入不会换行
            bufferedWriter.write(line);
            //换行
            bufferedWriter.newLine();

        }
        bufferedReader.close();
        bufferedWriter.close();
    }
}

 

 

 

二、字节流

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class IOTest {
    public static void main(String[] args) throws Exception {
        //copyByteFile();
        readFile();
    }

    /**
     * 读取字节文件-图片、视频、etc...
     */
    //用fileInputStream拷贝图片
    public static void copyByteFile() throws Exception {
        File sourceFile = new File("D:\\python27\\test.png");
        File targetFile = new File("D:\\python27\\lemon.png");
        //读取流
        FileInputStream fileInputStream = new FileInputStream(sourceFile);
        //写入流
        FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
        //定义一维byte数组 1024 -->每一次读取1024个字节的数据到数组里面
        //数据载体
        byte[] arr = new byte[1024];

        //文件的大小很多情况下都是会超过1024个字节,所以我们要多次去进行读取
        //length标识每一次读取到的长度
        int length = 0;
        //-1表示已经到达文件末尾
        while ((length = fileInputStream.read(arr)) != -1){
            //写入到文件里面
            //第一个参数:数组 第二个参数:写入的起始位置 第三个参数:写入的长度
            fileOutputStream.write(arr,0,length);
        }
        //关闭流
        fileInputStream.close();
        fileOutputStream.close();
    }

    //用fileInputStream读取文本文件
    public static void readFile() throws Exception {
        File File = new File("D:\\python27\\python27.txt");
        FileInputStream fileInputStream = new FileInputStream(File);
        byte[] arr = new byte[1024];
        int length = 0;
        while((length=fileInputStream.read(arr)) != -1){
            //打印的是地址
            //System.out.println(arr);
            /*for(int i =0;i <length; i++){
                System.out.println(arr[i]);
            }*/
            //读取的内容转成String  把字节数组 转换为字符串
            String str = new String(arr);
            System.out.print(str);
        }
        //关闭流
        fileInputStream.close();

      
    }
}

 

字节流 字符流区别:
1、读取的文件类型不同 字符流:普通文本文件 字节流:针对任意文件
2、字符流读取--》字符 字节流 --》字节
3、字符流=缓冲流BufferReader/BufferWriter;  字节流=也有缓冲流但用的比较少

posted @ 2020-07-03 15:07  mkay  阅读(152)  评论(0编辑  收藏  举报