20170824 Java——I/O,字节流与字符流,BufferedOutputStream,InputStream等(附相关练习代码)


I/O:

I/O是什么?

在程序中,所有的数据都是以流的形式进行传输或者保存。

程序需要数据的时候,就要使用输入流读取数据。

程序需要保存数据的时候,就要使用输出流来完成。

程序的输入以及输出都是以流的方式进行的,流中保存的为字节文件。

这里写图片描述


Java流:

流概念:

流与源数据和程序之间的关系:

源数据与程序时间是以流的形式传输的。

流与目标数据源和程序之间的关系:

程序与目标数据源之间以流的形式传输。

这里写图片描述

流的分类

按照流向划分:(相对程序而言)

输出流:OutputStream和Writer作为基类 (写入) 
输入流:InputStream和Reader作为基类 (读取)

按照处理数据单元划分: 
字节流:

字节输入流 
字节输出流

字符流:(文本一般以字符为单位)

字符输入流 
字符输出流

—-先看流向(入/出),在看处理单元(字符/字节)。

这里写图片描述

流的基类

流的基类:

输入流:InputStream(字节输入流)和Reader(字符输入流)为基类 
输出流:OutputStream(字节输出流)和Writer(字符输出流)为基类

这里写图片描述


InputStream

这里写图片描述

这里写图片描述

读取文件
public static void main(String[] args){
        //磁盘路径两种表示方式:
        //  1: \\   2: /
        try {
            //从文件地址中读取内容到程序中
            InputStream is = new FileInputStream("D:/IOFile/Ch02.txt");
            //开始读取信息
            //先定义一个字节数组存放数据
            byte[] b = new byte[8];
            //完整的读取一个文件
            int off=0;
            byte [] c=new byte[is.available()]; //返回文件的大小
            while(is.read(b,off,2)!=-1){
                off+=2; 
//              System.out.println(off);
            }
//          is.read(b,0,2);
//          is.read(b,off,len);
            //read返回读取的文件大小
            //最大不超过b.length,实际读取以文件大小为准
            //打印的字节
            System.out.println(Arrays.toString(b));
            //如何把字节数组转成字符串
            System.out.println(new String(b));
//          while(is.read(b)!=-1){
//              
//          }

            //关闭流
            is.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            //文件没有找到异常
            e.printStackTrace();
        } catch (IOException e) {
            //文件读写异常
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
返回文件的大小
byte [] b=new byte[is.available()];
   
   
  • 1
  • 1
读取文件:

BufferInputSream适用于大文件。

读取至一半时还可暂停。

FileInputStream fis = new FileInputStream("D:/IOFile/Ch05.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
//读取文件内容
byte [] b= new byte[bis.available()];
bis.read(b);
System.out.println(new String(b));
// String(byte[])把字节数组转成字符串
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

OutputStream

这里写图片描述

这里写图片描述

写入文件
public static void main(String[] args) {
        try {
            //把程序和目标源建立连接
            FileOutputStream fos = new FileOutputStream("D:/IoFile/out.txt");
            //把字符串转成字节数组
            String str = "求知若愚,虚心若饥";
            fos.write(str.getBytes());
            //flush 把数据完全冲刷到目标源中
            fos.flush();
            fos.close();
            System.out.println("文件写入成功!");

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

BufferedOutputStream

public static void main(String[] args) {
        try {
            //把程序和目标源建立连接
            //Buffer更加安全
            //直接传文件名,默认覆盖原有内容
            //文件名+true;在原有内容后追加新内容
//          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/IoFile/buffer.txt"));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/IoFile/buffer.txt",true));
            //把字符串转成字节数组
            String str = "求知若愚,虚心若饥";
            bos.write(str.getBytes());
            //flush 把数据完全冲刷到目标源中
            bos.flush();
            bos.close();
            System.out.println("文件写入成功!");

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

PrintStream

public static void main(String[] args) {
        //构造参数传System,out,就是在控制台打印信息
//      PrintStream ps = new PrintStream(System.out);
//      ps.print("132546u");
        try {
            PrintStream ps1=
                    new PrintStream
                    (new FileOutputStream("D:/IoFile/print.txt"));
            ps1.println("虚心若愚");
            ps1.println("求知若饥");
            ps1.println("求知若饥");
            ps1.println("虚心若愚");
            ps1.flush();
            ps1.close();
            System.out.println("写入成功!");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

DataInputStream

try {
            //写入
            DataOutputStream dos = new DataOutputStream(new FileOutputStream("D:/IoFile/data.txt"));
            dos.writeBoolean(true);
            dos.writeInt(110);
            dos.writeUTF("求知若饥");
            dos.flush();
            dos.close();
            System.out.println("写入成功!");

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //读取
        try {
            DataInputStream dis = new DataInputStream(new FileInputStream("D:/IoFile/data.txt"));
            //读的顺序必须与写的顺序相同
            dis.readBoolean();
            dis.readInt();
            dis.readUTF();
            System.out.println(dis.readBoolean());
            System.out.println(dis.readInt());
            System.out.println(dis.readUTF());
            dis.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一份 Java 输入输出流的示例代码,包括字节流字符流: 1. 字节流 字节流可以用于读写二进制文件或者文本文件,比如 InputStreamOutputStream。 ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class ByteStreamDemo { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream("input.txt"); FileOutputStream fos = new FileOutputStream("output.txt")) { byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1) { fos.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们使用 FileInputStream 读取 input.txt 文件中的字节流,使用 FileOutputStream 将读取到的字节流写入到 output.txt 文件中。其中,我们使用了 try-with-resources 语法,可以自动关闭资源,避免了手动关闭资源的麻烦。 2. 字符流 字符流可以用于读写文本文件,比如 Reader 和 Writer。 ```java import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class CharStreamDemo { public static void main(String[] args) { try (FileReader fr = new FileReader("input.txt"); FileWriter fw = new FileWriter("output.txt")) { char[] buffer = new char[1024]; int len; while ((len = fr.read(buffer)) != -1) { fw.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们使用 FileReader 读取 input.txt 文件中的字符流,使用 FileWriter 将读取到的字符流写入到 output.txt 文件中。同样地,我们使用了 try-with-resources 语法,可以自动关闭资源。 3. 缓冲流 为了提高读写性能,我们可以使用缓冲流,比如 BufferedInputStreamBufferedOutputStreamBufferedReader 和 BufferedWriter。 ```java import java.io.*; public class BufferedStreamDemo { public static void main(String[] args) { try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("input.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("output.txt"))) { byte[] buffer = new byte[1024]; int len; while ((len = bis.read(buffer)) != -1) { bos.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } } } import java.io.*; public class BufferedReaderDemo { public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new FileReader("input.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) { char[] buffer = new char[1024]; int len; while ((len = br.read(buffer)) != -1) { bw.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们使用了 BufferedInputStreamBufferedOutputStreamBufferedReader 和 BufferedWriter,提高了读写性能。同样地,我们使用了 try-with-resources 语法,可以自动关闭资源。 希望这份代码能够帮助到你。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值