IO流(字符流+缓冲字符流+字节流+缓冲字节流)

 

目录

字节流&&缓冲字节流

字符流&&缓冲字符流

 


字节流&&缓冲字节流

 

字节流输出写入数据

  • 一次写一个字节数据
public class file {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("fos.txt",true);
        fileOutputStream.write(99); 
        fileOutputStream.write(100);
        fileOutputStream.write(101);
        fileOutputStream.close();
   }
}
  • 一次写入一个字节数组的数据
public class file {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("fos.txt",true);
        byte[] by = {102,103,104};   //一次写一个字节数组(多个字节)
        byte[] by1 = "zp".getBytes();
        fileOutputStream.write(by,0,2);
        fileOutputStream.write(by1);
        fileOutputStream.close();
   }
}

字节输入流读取数据

  • 一次读取一个字节的数据
public class file {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("fos.txt");
        int line;
        while ((line = fileInputStream.read())!=-1){
            System.out.println(line);
        }
   }
}
  • 一次读取一个字节数组的数据
public class file {
    public static void main(String[] args) throws IOException {
       FileInputStream fileInputStream = new FileInputStream("fox.txt");
            byte[] line = new byte[10];
            fileInputStream.read(line);
            System.out.println(new String(line));
        }
   }
}

 

缓冲字节流写入数据

public class file {
    public static void main(String[] args) throws IOException {
       BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("fox.txt",true));
        bufferedOutputStream.write("woshinima".getBytes()); 
        bufferedOutputStream.close();
    }
}

缓冲字节流读取数据

public class file {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("fox.txt"));
        byte[] bytes = new byte[1024];
        bufferedInputStream.read(bytes);
        System.out.println(new String(bytes));
        bufferedInputStream.close();
    }
}


字符流&&缓冲字符流

 

为什么会出现字符流

因为字节流操作中文不是特别方便,所以java就提供字符流,使用字符流可以指定编码(GBK,UTF-8)

字符输出流写入数据

public class file {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("fox1.txt"),"GBK");
        outputStreamWriter.write("中国");
        outputStreamWriter.close();
    }
}

字符输入流读取数据

public class file {
    public static void main(String[] args) throws IOException {
       InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("fox1.txt"),"GBK");
        char[] bytes = new char[1024];
        int line;
        while ((line=inputStreamReader.read(bytes))!=-1){
            System.out.println( new String(bytes));
        }
        inputStreamReader.close();
    }
}

由于字符流是以字符为单位,所以读取的时候必须读取到字符数组,而字节流是以字节为单位,所以读取的时候必须读取到字节数组

 

字符缓冲流写入数据

public class file {
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter  = new BufferedWriter(new FileWriter("fox1.txt"));
        bufferedWriter.write("zzzzzz");
        bufferedWriter.close();
    }
}

字符缓冲流读取数据

 public class file {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("fox1.txt"));
        char[] chars = new char[100];
        int line;
        while ((line=bufferedReader.read(chars))!=-1){
            System.out.println(new String(chars));
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值