Java学习笔记 (二十) FileInputStream 和 FileOutputStream

FileInputStream

FileInputStream类能够以字节流的形式读取文件内容。FileInputStream是InputStream类的子类。
示例:

public static void main(String [] args) {
        FileInputStream in = null;
        try {
            in = new FileInputStream("千字文.txt");
            int i;
            byte [] bytes=new byte[34];
            // 读取字节到字节数组 
            while ((i = in.read(bytes)) != -1) {
                       //打印到控制台
                        System.out.println(new String(bytes,"utf-8"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (in!=null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

按字节读取文件内容 ,然后打印出来,为什么是34个字节呢。你可以获取千字文,尝试一下。千字文,点击获取

FileInputStream 的构造函数

FileInputStream(String name)

通过文件路径创建对象。

 public FileInputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null);
    }

可以看到 其实是构造方法的重载,调用了另一个构造方法。

FileInputStream(File file)

通过文件构造方法,上面的构造方法其实是调用了这个构造方法。

    public FileInputStream(File file) throws FileNotFoundException {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkRead(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        fd = new FileDescriptor();
        fd.attach(this);
        path = name;
        open(name);
    }

FileInputStream 的常用方法

read() 方法

read 方法返回一个int值,其中存储了读取的字节值。如果返回值为 -1 证明文本已经读完了,没有数据了。

为什么不返回byte呢

在这里插入图片描述
因为返回字节值的范围是 -1 ~ 255, byte 存不了。
其实我蛮好奇 为什么不用short

read(byte[]) 方法

read(byte[])方法 一次读取一个字节数组,返回int值存储了读取的字节数,如果读完了就会返回-1,使用方法参考上面的代码示例。

FileOutputStream

FileOutputStream 可以以字节流的形式写入数据到文件。FileOutputStream是OutputStream的子类。

    public static void main(String []args){
        FileOutputStream os=null;

        try {
            os=new FileOutputStream("outTest.txt");
            os.write("I Love".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

FileOutputStream 的构造函数

FileOutputStream(String name)

通过文件路径构造对象。

FileOutputStream(String name, boolean append)

通过文件路径构造对象,多了一个参数,
append =true时,会原有的文件内容后面添加
append =false时, 会覆盖文件。
其他构造方法 默认append为 false.

FileOutputStream(File file)

通过文件构造对象

FileOutputStream 常用方法

public void write(int b)

写入 一个int值。包含要写入的字节值。

public void write(byte b[])

写入一个字节数组

flush()

将数据写入FileOutputStream时,数据可能会被缓存在计算机内存中并在稍后写入磁盘。调用flush()将确保到目前为止写入FileOutputStream的所有数据也全部写入磁盘。

参考资料

1.Java IO Tutorial
2.The Java™ Tutorials

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值