Java 学习IO流

流是什么

流是一种抽象概念,它代表了数据的无结构化传递。按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列。从流中取得数据的操作称为提取操作,而向流中添加数据的操作称为插入操作。用来进行输入输出操作的流就称为IO流。换句话说,IO流就是以流的方式进行输入输出

流的分类

字节流字符流
输入流InputStreamReader
输出流OutputStreamWriter

 File类的概述

常用的构造方法public File(String path)

public boolean createNewFile () 创建文件
public boolean mkdir () 创建单文件夹
public boolean mkdirs () 多级文件夹创建
public boolean delete () 删除
public boolean renameTo ( File dest )) 重命名功能
public boolean isDirectory () 是否是文件夹
public boolean isFile () 是否是文件
public boolean exists () 是否存在

 字节输入流

字节输入流InputStream抽象类,不能实例化
构造方法:
FileInputStream(String name) throws FileNotFoundException 创建一个字节输入流对象,参数就是指定要读取的文件的路径
读数据的方式:
方式1:public int read() throws IOException :一次读取一个字节,返回的实际的字节数

方式2:一次读取一个字节数组public int read(byte[] b)throws IOException

FileInputStream fis = new FileInputStream("a.txt") ;//读取当前项目下的a.txt文件
int by = 0 ;
        while((by=fis.read())!=-1){
            //一直读,展示数据
            System.out.print((char)by); //将字节数--->字符
        }
//一次读取一个字节数组,效率高
 byte[] bytes = new byte[1024] ;
        int len = 0 ;//实际字节是从0开始
        while((len=fis.read(bytes))!=-1){
            //一直读,每次是从0角标开始读取实际字节数,构造成字符串展示
            System.out.println(new String(bytes,0,len));

        }

 字符转换输入流:字节输入流转换字符输入的桥梁,InputStreamReader是Reader的字符流的具体类

构造方法 InputStreamReader(InputStream in)

public class InputStreamReaderDemo {

    public static void main(String[] args) throws Exception {
        //不带字符集格式的这个构造方法,默认就是平台字符集
        InputStreamReader isr = new InputStreamReader(
                new FileInputStream("a.txt")) ;
        char[] chs = new char[1024] ;
        int len = 0 ; //实际字符数
        while((len=isr.read(chs))!=-1){
            //展示控制台上
            System.out.println(new String(chs,0,len));
        }

        //释放资源
        isr.close();
    }
}

 字节缓冲输入流BufferedInputStream不能直接操作文件,操作文件需要底层字节流
 构造方法

        public BufferedInputStream(InputStream in)

public class BufferedInputStreamDemo {
    public static void main(String[] args) throws Exception {
        BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream("c.txt")) ;
 byte[] bytes = new byte[1024] ;
        int len  = 0 ;//实际字节数
        while((len=bis.read(bytes))!=-1){
            //输出内容
            //每次从0开始将字节数转换成字符串
            System.out.println(new String(bytes,0,len));
        }
        //释放资源
        bis.close();
    }
}

字节输出流

创建一个字节输出流对象FileOutputStream(File file)throws FileNotFoundException

写数据的方法
public void write(byte[] bytes)
public void write(byte[] bytes,int offset,int length)
public void write(int n)

        byte[] bytes = {97,98,99,101,109} ;
        fos.write("hello,world".getBytes());
        fos.write(bytes,2,2);
        fos.write(101);
        for(int x = 0 ; x <100;x++){
            fos.write(("hello"+x).getBytes());
        }

 以上写法会覆盖原来的数据内容

在源文件上追加内容的写法

FileOutputStream fos = null;

        try {
            //创建字节输出流对象FileOutputStream
            //FileOutputStream fos = new FileOutputStream("a.txt") ;
            //ublic FileOutputStream(String name,boolean append)throws FileNotFoundException
            fos = new FileOutputStream("a.txt",true);

            //循环写入
            for(int x = 0 ; x < 15 ; x ++){
                fos.write(("hello"+x).getBytes()); //写数据
                //每次写一个换行一个
                fos.write(("\r\n").getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

字符转换输出流:OutputStreamWriter  :字节输出流通向字符输出流的桥梁

构造方法:public OutputStreamWriter(OutputStream out)

                public OutputStreamWriter(OutputStream out,String charsetName)

写入内容的方法:

                void write(char[] buf):写入字符数组

                abstract void write(char[] cbuf, int off, int len) :写字符数组的一部分

                void write(String str):写入字符串 

public class OutputStreamWriterDemo {
    public static void main(String[] args) throws Exception {
       OutputStreamWriter osw = new OutputStreamWriter(
                new FileOutputStream("b.txt")) ;//创建字符转换输出流对象
        String s = "这是个测试,哈哈哈" ;
        osw.write(s);
        osw.flush();//刷新
        osw.close();//释放资源
    }
}

字节缓冲输出流BufferedOutputStream

构造方法

                public BufferedOutputStream(OutputStream out)

public class BufferedOutputStreamDemo {

    public static void main(String[] args) throws Exception {
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("d.txt")) ;//创建默认缓冲区大小的字节缓冲流对象
        bos.write("hello,字节缓冲流".getBytes());//写数据
        //释放资源
        bos.close();
    }
}

字符缓冲输入流BufferedReader高效读取

构造方法

                BufferedReader(Reader in)

public String readLine() throws IOException一次读取一行内容,它如何判断文件是否到达末尾呢?返回值为null,表示文件读取完毕

public class BufferedReaderDemo {
    public static void main(String[] args) throws Exception {
        //创建字符缓冲输入流对象
        BufferedReader br = new BufferedReader(new FileReader("bw.txt")) ;
        String line = null ;
        while((line= br.readLine())!=null){
            //判断,赋值,调用方法一块用  readLine()也是阻塞式方法,只要没有null值,它是一直读
            System.out.println(line);
        }
        //释放资源
        br.close();
    }
}

字符缓冲输入流BufferedWriter高效写入

构造方法:

                BufferedWriter(Writer out) :创建默认缓冲区大小的字符缓冲输出流

写入方法:

        write(String str)

public void newLine() throws IOException :写入一个行终止符号,就是换行

public class BufferedWriterDemo {
    public static void main(String[] args) throws Exception {
        //BufferedWriter(Writer out) :创建默认缓冲区大小的字符缓冲输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt")) ;
                //写入字符串
        bw.write("hello");

        // public void newLine() throws IOException :写入一个行终止符号,就是换行
        bw.newLine();
        bw.write("world");
        bw.newLine();
        bw.write("javaEE");
        bw.newLine();

        //刷新
        bw.flush();
        //释放资源
        bw.close();
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值