(七)字节流详解


基本的原理、方法等与字符流类似,因此字节流相对会精简一些
如果还有疑问请参考字符流详解

1、使用字节输出流向文件中写入数据

import java.io.FileOutputStream;
import java.io.IOException;

public class Demo1 {
public static void main(String[] args)  throws IOException{
          writeFile();
}

        public static void writeFile() throws IOException{
        //创建字节输出流对象和文件相关联
        FileOutputStream fos = new FileOutputStream("F:\\Java.txt");
        //直接写入文件
        fos.write("abcde".getBytes());
        //转成的数组为[97,98,99,100,101]
        fos.close();
        }


2、使用字节输入流读取文件中的数据——单个字节

import java.io.FileInputStream;
import java.io.IOException;

public class Demo2 {
public static void main(String[] args)  throws IOException{
          readFile();
}

        public static void readFile() throws IOException{
        FileInputStream fis = new FileInputStream("F:\\Java.txt");
        //每次读取单个字节
        int num;
        while((num = fis.read())!=-1){//读到文件末尾返回-1
            //num是int类型,注意转换
            System.out.print((char)num);
            }
        fis.close();
        }



3、使用字节输入流读取文件中的数据——字节数组

import java.io.FileInputStream;
import java.io.IOException;

public class Demo3 {
public static void main(String[] args)  throws IOException{
          readFile2();
}

        public static void readFile2() throws IOException{
        FileInputStream fis = new FileInputStream("F:\\Java.txt");
        byte[] arr = new byte[1024];
        int num;
        while((num = fis.read(arr))!=-1){//读到文件末尾返回-1
            //将结果转换字符串
            System.out.print(new String(arr,0,num));
            }
        fis.close();
        }


4、使用字节输入流读取文件中的数据——优化字节数组

import java.io.FileInputStream;
import java.io.IOException;

public class Demo4 {
public static void main(String[] args)  throws IOException{
          readFile3();
}
        public static void readFile3() throws IOException{
        FileInputStream fis = new FileInputStream("F:\\Java.txt");
        //返回文件的总大小,单位是字节
        int num = fis.available();  
        byte[] arr = new byte[num];
        /*
         * 当数组长度和文件总大小相同时
         * 读取一次即可
         * 不再需要循环读取
         * 但文件太大时不适合用
         * 以防内存溢出
         */
        int len = fis.read(arr);
        System.out.print(new String(arr,0,len));
        }
}   

5、复制一张图片——每次读取单个字节

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

public class Demo5 {
public static void main(String[] args){
          copy1();
}
    
            public static void copy1() throws IOException{
            FileInputStream fis = new FileInputStream("F:\\java.jpg");
            FileOutputStream fos = new FileOutputStream("F:\\java_copy1.jpg");
            int num;
            while((num = fis.read())!=-1){
                fos.write(num);
                }
            fis.close();
            fos.close();
            }


6、复制一张图片——使用数组读取并处理异常

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

public class Demo6 {
public static void main(String[] args){
          copy2();
}
            public static void copy2(){
            FileInputStream fis =null;
            FileOutputStream fos = null;    
                try {
                    fis = new FileInputStream("F:\\java.jpg");
                    fos = new FileOutputStream("F:\\java_copy2.jpg");
                    byte[] arr = new byte[1024];
                    int num;
                    while((num = fis.read(arr))!=-1){
                        fos.write(arr,0,num);
                    }       
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{
                    if(fis!=null){
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(fos!=null){
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }   
            }


7、复制一张图片——使用字节缓冲流读取并处理异常

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo7 {
public static void main(String[] args){
          copy3();
}
    
    public static void copy3(){
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;    
    try {
        bis=new BufferedInputStream(new FileInputStream("F:\\java.jpg"));
        bos = new BufferedOutputStream(new FileOutputStream("F:\\java_copy3.jpg"));
         byte[] arr = new byte[1024];
         int num;
         while((num = bis.read(arr))!=-1){
             bos.write(arr,0,num);
         }      
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        try {
            if(bis!=null){
                bis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }       
        try {
            if(bos!=null){
                bos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

版权声明:欢迎转载,欢迎扩散,但转载时请标明作者以及原文出处,谢谢合作!             ↓↓↓
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,099评论 19 139
  • 《ilua》速成开发手册3.0 官方用户交流:iApp开发交流(1) 239547050iApp开发交流(2) 1...
    叶染柒丶阅读 11,072评论 0 11
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,242评论 6 13
  • 6月22号以来,看似寻常的六月天,对于每一个已经结束高中生活的你们来说,却丝毫也寻常不起来。 6月24号—曾经我以...
    邹虫虫阅读 303评论 0 0
  • 食物是有生命的,应时而作,应时而食,方能恰到好处的感受食物的灵性,感受生命的美好。 四季轮转,花开花谢,采撷一掬花...
    羚羊漫步阅读 330评论 1 2