Java 学习笔记- I/O - Byte Stream

java basic IO 主要分 字节输入输出流,字符输入输出流,数据输入输出流,对象输入输出流:

 其大概的类接口结构图如下:


 关于字节输入输出流,所以字节输入输出流的类都继承了FileInput 或 FileOutput 这两个父类,常用的类为 FileInputStream 和 FileOutputStream,常用的构造方法为 FileInputStream(String name), FileOutputStream(String name),或 FileInputStream(File name),FileOutPutStream(File  name).

此外还有类 BufferedInputStream 与 BufferedOutputStream 也为常用的字节输入输出流类,其构造方法为 BufferedInputStream(InputStream in), BufferedOutputStream(OutputStream out);使用这两个类进行I/O操作可以减少读写的次数,从而提高程序的效率。

 

Oracle官网上用字节I/O读写文件的示例代码:

 

package io.bytestream;

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

/**
 * IO Stream
 * 
 * 1. Byte Streams
 * 
 * All byte stream class are descended from InputStream and OutputStream.
 * 
 * @author PENGGR
 * 
 */
public class CopyBytes {

    public static void main(String[] args) {
        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            in = new FileInputStream("D:\\360云盘\\javase\\src\\io\\xanadu.txt");
            out = new FileOutputStream("D:\\360云盘\\javase\\src\\io\\byteStreamOut.txt");
            int c;
            while ((c = in.read()) != -1) {
                out.write(c);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                    if (out != null) {
                        out.close();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值