java IO流

java IO流

字节流

InputStream/OutputStream(抽象类)
文件流:
    FileInputStreamread()
    	文件输入流,将文件中的内容读取到程序中
    FileOutputStreamwrite()	
    	文件输出流,将程序中的内容写到文件中
缓冲流:
    在内存中开辟一段缓冲区,减少对硬盘的操作,提高效率
    缓冲区中的内容由系统自动刷新,当缓冲取满了之后,将缓冲区的内容一次性写入到文件中
  
    BfferedInputStream:缓冲字节输出流
    	read()
    BfferedOutputStream:基于文件输出流的基础上进行包装
		write()

文件流

 FileInputStream/FileOutputStream
文件输入流
public static void main(String[] args) {
    File file = new File("D:/Desktop/test/test01.txt");
    try {
        if (!file.exists()) {
            file.createNewFile();
        }
        //文件输出流
        //默认false为覆盖写,true为追加写
        OutputStream o = new FileOutputStream(file,true);
        //写一个字节
        o.write('a');
        //写一个字节数组
        String str = "fadskj;as'dsfzsdfi";
        o.write(str.getBytes());
        //写字节数组,从字节数组指定位置开始写,写多少个字节
        o.write(str.getBytes(),0,str.length());
        o.close();//这条流用完需关闭

    } catch (IOException e) {
        e.printStackTrace();
    }

}
文件输出流
public static void main(String[] args) {
    File file = new File("D:/Desktop/test/test01.txt");
    if (!file.exists()) {//文件不存在,退出读取
        return;
    }
    try {
        InputStream i = new FileInputStream(file);
        //读取文件
        //读取一个字节
        int read = i.read();
        System.out.println((char)read);
        //读取字节数组
        byte[] b = new byte[2048];
        int read2 = i.read(b);//返回值表示读取到的字节量
        System.out.println(read2);
        String str = new String(b,0,read2);//转为字符串,从0开始,到它的有效长度结束
        System.out.println(str);
        //读取字节数组,从字节数组指定位置开始,存入多少个
        int read3 = i.read(b,100,50);
        System.out.println(read3);//返回值如果是-1,表示已经读取到文件末尾,没有内容可读

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}
文件流复制
public static void main(String[] args) {
    File file1 = new File("D:/Desktop/test/test01.txt");
    File file2 = new File("D:/Desktop/test/test01_copy1.txt");
    if (!file1.exists()) {
        return;
    }
    if (!file2.exists()) {
        try {
            file2.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream fos = new FileOutputStream(file2);
        byte[] b = new byte[2048];
        while(true){
            int read = fis.read(b);
            if (read == -1) {
                break;
            }
            fos.write(b, 0, read);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

缓冲字节流

BfferedInputStream/BfferedOutputStream
缓冲输入流
public static void main(String[] args) {
    File file = new File("D:/Desktop/test/test001.txt");
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        //从缓冲区写出到文件的内容,是通过文件流完成的
        FileOutputStream fos = new FileOutputStream(file,true);
        //读写面对是缓冲区
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //当缓冲区写满之后,才会将内容写到文件中
        //写一个字节
        bos.write('a');
        //写一个字节数组
        bos.write("dfaasdfa".getBytes());
        bos.flush();//手动将缓冲区的内容写到文件中
        bos.close();//关闭流对象时,只需要关闭最上层的流,底层会自动关闭,会自动清空缓存区,可以不用bos.flush,数据也会写进文件

    } catch (Exception e) {
        e.printStackTrace();
    }
}
缓冲输出流
public static void main(String[] args) {
    File file = new File("D:/Desktop/test/test001.txt");
    if (!file.exists()) {
        return;
    }
    try (
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
    ){
        int read = bis.read();
        System.out.println(read);
        byte[] b = new byte[2048];
        int read2 = bis.read(b);
        String str = new String(b,0,read2);
        System.out.println(str);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
缓冲流复制
public static void main(String[] args) {
    File file1 = new File("D:/Desktop/test/test01.txt");
    File file2 = new File("D:/Desktop/test/test01_copy01.txt");
    if (!file1.exists()) {
        return;
    }
    if (!file2.exists()) {
        try {
            file2.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try (
        FileInputStream fis = new FileInputStream(file1);
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream(file2);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
    ){
        byte[] b = new byte[2048];
        while(true){
            int read = fis.read(b);
            if (read == -1) {
                break;
            }
            fos.write(b, 0, read);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

用完关闭类的方式

//关闭方式一
FileOutputStream fos = null;
try {
    fos = new FileOutputStream("test.txt");
} catch (FileNotFoundException e) {
    e.printStackTrace();
}finally {
    //不管try和catch中的内容是否执行,finally中的代码一定会被执行
    try {
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
//关闭方式二
//小括号()里的类,用完会自动关闭
try (//JDK1.7以上才支持
    //该类必须实现Closeable
    FileOutputStream fos2 = new FileOutputStream("test.txt");
    BufferedOutputStream bos = new BufferedOutputStream(fos2);
){

} catch (Exception e) {
}

字符流

Reader/Writer
字符流:   
    InputStreamReader/OutputStreamWriter
缓冲字符流
    BufferedReader/PrintWriter

字符输入流

public static void main(String[] args) {
    File file = new File("D:/Desktop/test/test002.txt");
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try (
        FileOutputStream fos = new FileOutputStream(file,true);
        OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");//GBK:中文以2个字节存储、UTF-8:使用3个字节存中文
    ){
        osw.write("adskfjdfifasijenii");
        String str = "法律等放假安排骄傲的风景";
        osw.write(str.toCharArray());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

字符输出流

public static void main(String[] args) {
    File file = new File("D:/Desktop/test/test002.txt");
    if (!file.exists()) {
        return;
    }
    try (
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis,"GBK");
    ){
        int read = isr.read();
        char[] c = new char[2048];
        int read2 = isr.read(c);
        String str = new String(c,0,read2);
        System.out.println(str);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

缓冲字符流

BufferedReader:缓冲字符输入流
PrintWriter:缓冲字符输出流
可以实现数据的按行读写    
缓冲字符输出流
public static void main(String[] args) {
    File file = new File("D:/Desktop/test/test1009.txt");
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try (
        FileOutputStream fos = new FileOutputStream(file);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
        PrintWriter pw = new PrintWriter(osw,true);//表示是否需要按行刷新缓冲区(true刷新/false不刷新),pw.println()才有效果,pw.print()没有该效果
    ){
        pw.println("afsdfalskdfjoiasjj");
        pw.println("地方静安寺女你打开复返");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
缓冲字符输入流
public static void main(String[] args) {
    File file = new File("./src/_20211009/Demo04.java");
    if (!file.exists()) {
        return;
    }
    try (
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis, "GBK");
        BufferedReader br = new BufferedReader(isr);
    ){
      //读取一行
        //String readLine = br.readLine();
        //System.out.println(readLine);
      //循环读取所有行
        String readLine = null;
        while((readLine = br.readLine()) != null){
            System.out.println(readLine);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

抹泪的知更鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值