字节型文件输入流、字节型文件输出流

本文主要内容:

(1)字节型文件输入流
(2)字节型文件输出流

1.file对象不能操作文件中的内容——>通过流I/O的方式来完成
2.流按照方向(功能)来区分:in(读取)、out(写入)
3.操作的目标来区分:文件流、数组流、字符串流、数据流、对象流、网络流
4.文件流:读取文件中的信息(in)、将信息写入文件中(out);文件流按照读取或写入的单位(字节数)大小来区分,可以分为字节型文件流(1字节):FileInputStream/FileOutputStream、字符型文件流(2字节—>1字符):FileReader/FileWriter
5.容器:
(1)变量:只能存一份
(2)数组:存储多个、数据类型统一
(3)集合:存储多个、存储后个数还能改变,泛型——数据类型统一
如上三个都是JAVA中的类型,对象存储在内存。都存储在内存中,程序执行完毕,虚拟机停止时,内存空间就要回收,数据都是临时性存储。
6.文件:存储很多信息,文件都是存储在硬盘上的——>永久保存,数据虽然是安全了,文件毕竟不在内存中,需要通过IO操作文件。
7.字节型文件输入流(FileInputStream
(1)java.io包
(2)继承于InputStream类
(3)创建对象:调用一个带File类型的构造方法、调用一个带String类的构造方法
(4)常用方法:
int code = read();每次从流道中读取恶一个字节,返回字节的code码
int count = read(byte[]);每次从流道中读取额若干个字节,存入数组内返回有效元素
int count = available();返回流管道中还有多少缓存的字节数
skip(long n);跳过几个字节读取
close();将流管道关闭—必须要做,最好放在finally里,注意代码的健壮性,判断严谨

//读取一个字节
public static void main(String[] args) {
    try {
        //创建一个字节型的文件输入流,读取一个文件的内容
        File file = new File("D://test//Test.txt");
        //读文件
        FileInputStream fis = new FileInputStream(file);
        try {
            int i = fis.read();//读取一个字节
            System.out.println(i);//读取的字符对应的Unicode码
            //输出结果97
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
//全部读取
public static void main(String[] args) {
    try {
        //创建一个字节型的文件输入流,读取一个文件的内容
        File file = new File("D://test//Test.txt");
        //读文件
        FileInputStream fis = new FileInputStream(file);
        try {
            int i = fis.read();//读取一个字节
            while (i!=-1){
                System.out.print(i+" ");//读取的字符对应的Unicode码
                i = fis.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
//创建一个数组,去文件里读东西,装入数组内,读取到的有效字节个数
try {
    //创建一个字节型的文件输入流,读取一个文件的内容
    File file = new File("D://test//Test.txt");
    //读文件
    FileInputStream fis = new FileInputStream(file);
    byte[] b = new byte[5];//创建一个空的数组——>小推车
    int count = fis.read(b);//去文件里读东西,装入数组,读取到有效字节的个数
    while (count!=-1){
        //System.out.println("----"+count);
        //String value = new String(b);
        String value = new String(b,0,count);
        System.out.print(value);
        count = fis.read(b);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
public static void main(String[] args) {
    FileInputStream fis = null;
    try {
        fis = new FileInputStream("D://test//Test.txt");
        fis.skip(2);
        int code = fis.read();
        System.out.println((char)code);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(fis!=null){
            try {
                fis.close();//关闭流通道,流一定要关闭
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

8.字节型文件输出流
(1)FileOutputSream:将数据写入文件
(2)java.io包
(3)创建对象:调用一个带File参数的(带File、boolean重载)、调用一个带String参数(带String、boolean的重载)
(4)常用方法:
write(int code);将给定code的对应的字符写入文件
write(byte[]);将数组中的全部字节写入文件
flush();将管道内字节推入(刷新)文件
close();注意在finally中关闭

public class Test1 {
    public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream(new File("D://test//Test.txt"),true);
            //创建的是文件输入流,若文件路径有问题,则抛出异常
            //创建的是文件输出流,若文件路径有问题,则直接帮我们创建一个新的文件
            fos.write(97);
            fos.write(98);
            fos.flush();
            System.out.println("写入完毕");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class Test2 {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File("D://test//Test.txt"),true);
            byte[] b = new byte[]{97,98,99};
            fos.write(b);
            fos.flush();
            System.out.println("写入完毕");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值