Java高级Day32-字节输入流

87.InputStream字节输入流

InputStream抽象类是所有类字节输入流的超类,常用子类有:

  1. FileInputStream:文件输入流

  2. BufferedInputStream:缓冲字节输入流

  3. ObjectInputStream:对象字节输入流

FileInputStream

单个字节读取

//要求使用FileInputStream读取hello.txt文件,并将文件内容显示到控制台
public class FileInputStream_ {
    public static void main(String[] args){
        
    }
    
    public void readFile01(){
        String FilePath = "d:\\hello.txt";
        int readData = 0;
        FileInputStream fileInputStream = null;
        try{
            //创建了 FileInputStream 对象用于读取文件
            fileInputStream = new FileInputStream(filePath);
            //从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止
            //如果返回 -1,表示读取完毕
            while((readData = fileInputStream.read()) != -1){
                System.out.println((char)readData);//转成char
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            //关闭文件流,释放资源
            try{
                fileInputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

使用read(byte[] b)读取

public class FileInputStream_ {
    public static void main(String[] args){
        
    }
    
    public void readFile01(){
        String FilePath = "d:\\hello.txt";
        //字节数组
        byte[] buf = new byte[8];//一次读取8个字节
        FileInputStream fileInputStream = null;
        try{
            //创建了 FileInputStream 对象用于读取文件
            fileInputStream = new FileInputStream(filePath);
            //从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止
            //如果返回 -1,表示读取完毕
            //如果读取正常,返回实际读取的字节数
            while((readLen = fileInputStream.read(buf)) != -1){
                System.out.println(new String(buf,0,le));//显示 把buf转成字符串
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            //关闭文件流,释放资源
            try{
                fileInputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

88.FileOutputStream

//使用FileOutputStream将数据写到文件中,若文件不存在,则创建该文件
public class FileOutputStream{
    public static void main(String[] args){
    
    }
    public void writeFile(){
        //创建 FileOutputStream对象
        String filePath = "d:\\a.txt";
        FileOutputStream fileOutputStream = null;
        
        try{
            //得到FileOutputStream对象
            //说明:
            //1.如果new FileOutputStream(filePath);创建方式,当写入内容时,会覆盖原来的内容
            //2.new FileOutputStream(filePath,true);创建方式,写入内容是追加到末尾
            fileOutputStream = new FileOutputStream(filePath);
            //写入一个字节
            fileOutputStream.write('a');
            //写入字符串
            String str = "hello,world!";
            //str.getBytes() 可以把 字符串->字节数组
            //write(byte[] b,int off,int len)将len字节从位于偏移量off的指定字节数组写入此文件输出流
            fileOutputStream.write(str.getBytes());
        } catch (IOException e){
            e.printStackTrace();
        } finally {
            fileOutputStream.close();
        }
    }
}

89.文件拷贝

//编程完成 图片/音乐 的拷贝
public class FileCopy{
    //完成 文件拷贝,将 d:\\Koala.jpg 拷贝 c:\\
    //1.创建文件的输入流,将文件读入到程序
    //2.创建文件的输出流,将读取到的文件数据,写入到指定的文件
    String srcFilePath = "d:\\Koala.jpg";
    String desFilePath = "c:\\Koala2.jpg";
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    
    try{
        fileInputStream = new FileInputStream(srcFilePath);
        fileOutputStream = new FileOutputStream(desFilePath);
        //定义一个字节数组,提高读取效果
        byte[] buf = new byte[1024];
        int readLen = 0;
        while((readLen = fileInputStream.read(buf)) != -1){
            //读取到后,就写入到文件 通过 fileOutputStream
            //即,是一边读,一边写
            fileOutputStream.write(buf,0,readLen);
        }
        System.out.println("拷贝成功")
    } catch (IOException e){
        e.printStackTrace();
    }finally(fileInputStream != null) {
        try{
           //关闭输入流和输出流,释放资源
           if(fileIntputStream != null){
               fileInputStream.close();
           }
           if(fileOutputStream != null){
               fileOutputStream.close();
           }
    }
}

90.FileReader 和 FileWriter 介绍

FileReader 和 FileWriter 是字符流,即按照字符来操作io

FileReader相关方法:

  1. new FileReader(File/String)

  2. read:每次读取单个字符,返回该字符,如果文件末尾返回到 -1

  3. read(char[]):批量读取国歌字符到数组,返回读取到的字符数,如果到文件末尾返回-1

相关API:

  1. new String(char[]):将char[]转换成String

  2. new String(char[],off,len):将char[]的指定部分转换成String

FileWriter常用方法

  1. new FileWriter(File/String):覆盖模式,相当于流的指针在首端

  2. new FileWriter(File/String,true):追加模式,相当于流的指针在尾端

  3. write(int):写入单个字符

  4. write(char[]):写入指定数组

  5. write(char[],off,len):写入指定数组的指定部分

  6. write(string):写入整个字符串

  7. write(string,off,len):写入字符串的指定部分

相关API:String类:toCharArray:将String转换成char[]

注意:FileWriter使用后,必须要关闭(close)或刷新(flush),否则写入不到指定的文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值