Java—IO流(字节流)

Java—IO流(字节流)

1、IO流的概述及其分类

(1)IO流用来处理设备之间的数据传输
(2)java对数据的操作是通过流的方式
(3)java用于操作流的类都在IO包中
    按方向分为:输入流和输出流。简单理解就是:以内存为主,用来操作将硬盘已有的数  
据读取  到内存的是输入流,用来 操作将内存中的数据存储到硬盘中的是输出流。输入流进  
行读操作,输出流进行写操作。

2、字节流字符流

     字节流是由字节组成的,字符流是由字符组成的。Java里字符由两个字节组成。字节 
 流是最基本的,所有的InputStream和OutputStream的子类都是,主要用在处理二进制  
 数据,它是按字节来处理的,但实际中很多的数据是文本,又提出了字符流的概念,它是按  
 虚拟机的encode来处理,也就是要进行字符集的转化。在从字节流转化为字符流时,实际  
 上就是byte[]转化为String时,public String(byte bytes[],String   
 charsetName)有一个关键的参数字符集编码,通常我们都省略了,那系统就用操作系统默  
 认的long。
 操作步骤:选择管道→创建管道→读写操作→关闭管道

IO代码展示,运行结果读者可以自己修改代码中的文件存储路径进行查看。

package com.hwadee;

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

public class TestIO1 {

    public static void main(String[] args) throws IOException {
//      test1();
//      test2();
//      test3();
//      test4();
//      test5();
//      test6();
//      test7();
//      test8();
//      test9();
        test10();
    }
    public static void test1() throws IOException {
        File file=new File("D:\\hhhh.txt");
        boolean isSuc = file.createNewFile();
        System.out.println(isSuc);
    }
    public static void test2() throws IOException {
        FileInputStream fis =new FileInputStream("D:\\hhhh.txt");
        int ch=0;
        while((ch=fis.read())!=-1) {
            System.out.print((char)ch);
        }
        fis.close();

    }
    public static void test3() throws IOException {
        FileInputStream fis =new FileInputStream("D:\\hhhh.txt");
        byte[] b=new byte[10];
        int len=0;
        while((len=fis.read(b))!=-1) {
            String s=new String(b, 0, len);
            System.out.println(b.length);
            System.out.println(s);
        }
        fis.close();
    }
    public static void test4() throws IOException {
        FileOutputStream fos=new FileOutputStream("D:\\hhhh.txt");
        fos.write(97);
        fos.close();
    }
    public static void test5() throws IOException {
        FileOutputStream fos=new FileOutputStream("D:\\hhhh.txt");
        byte[] b= {97,98,99,100};
        fos.write(b);
        fos.close();
    }
    public static void test6() throws IOException {
        FileOutputStream fos=new FileOutputStream("D:\\hhhh.txt");
        String s="你们好!";
        fos.write(s.getBytes());
        fos.close();
    }
    public static void test7() throws IOException {
        FileInputStream fis=new FileInputStream("D:\\hhhh.txt");
        byte[] b=new byte[1024*1];
        int len=0;
        FileOutputStream fos=new FileOutputStream("D:\\copyhhhh.txt");
        while((len=fis.read(b))!=-1) {

            fos.write(b, 0, len);
        }
        System.out.println("copy完成");
        fis.close();
        fos.close();
    }
    public static void test8() throws IOException {
        FileInputStream fis=new FileInputStream("D:\\lyf.jpg");
        byte[] b=new byte[1024*1000];
        int len=0;
        FileOutputStream fos=new FileOutputStream("D:\\copylyf.jpg");
        while((len=fis.read(b))!=-1) {
            System.out.println("-----------------");
            fos.write(b, 0, len);
        }
        System.out.println("copy完成");
        fis.close();
        fos.close();
    }
    public static void test9() throws IOException {
        FileInputStream fis=new FileInputStream("D:\\舞动的排序算法 冒泡排序.flv");
        byte[] b=new byte[1024*1];
        int len=0;
        FileOutputStream fos=new FileOutputStream("D:\\copy舞动的排序算法 冒泡排序.flv");
        while((len=fis.read(b))!=-1) {
            fos.write(b, 0, len);
        }
        System.out.println("copy完成");
        fis.close();
        fos.close();
    }
    public static void test10() throws IOException {
        FileOutputStream fos=new FileOutputStream("D:\\hhhh.txt",true);
        String s="今天很冷";
        fos.write(s.getBytes());
        fos.close();
    }
}

3、异常处理后的代码再次展示。

package com.hwadee;

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

public class TestIO2 {

    public static void main(String[] args) {
//      test1();
        test2();
    }
    // java.lang.NullPointerException 空指针异常
    public static void test1() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("D:\\qqq.txt");
            String s = "大噶好,我是渣渣辉";
            fos.write(s.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void test2() {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:\\qqq.txt");
            fos = new FileOutputStream("D:\\copyqqq.txt");
            byte[] b = new byte[1024 * 1];
            int len = 0;
            while ((len = fis.read(b)) != -1) {
                fos.write(b, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null && fis != null) {
                    fis.close();
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

4、BufferedInputStream与BufferedOutputStream
同样操作下的代码展示。

package com.hwadee;

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

public class TestIO3 {

    public static void main(String[] args) {
//      test1();
//      test2();
//      test3();
        test4();
    }
    public static void test1(){
        FileInputStream fis=null;
        BufferedInputStream bis=null;
        try {
            fis=new FileInputStream("D:\\hhhh.txt");
            bis=new BufferedInputStream(fis);
            byte[] b=new byte[1024*1];
            int len=0;
            while((len=bis.read(b))!=-1) {
                String s=new String(b, 0, len);
                System.out.println(s);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void test2() {
        FileOutputStream fos=null;
        BufferedOutputStream bos=null;
        try {
            fos=new FileOutputStream("D:\\hhhh.txt",true);
            bos=new BufferedOutputStream(fos);
            String s=",你是最美的星星";
            bos.write(s.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void test3() {
        FileInputStream fis=null;
        BufferedInputStream bis=null;
        FileOutputStream fos=null;
        BufferedOutputStream bos=null;
        try {
            fis=new FileInputStream("D:\\lyf.jpg");
            bis=new BufferedInputStream(fis);
            fos=new FileOutputStream("D:\\copylyf1.jpg");
            bos=new BufferedOutputStream(fos);
            int len=0;
            while((len=bis.read())!=-1) {
                bos.write(len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }   
        }
    }
    public static void test4() {
        FileInputStream fis=null;
        BufferedInputStream bis=null;
        FileOutputStream fos=null;
        BufferedOutputStream bos=null;
        try {
            fis=new FileInputStream("D:\\舞动的排序算法 冒泡排序.flv");
            bis=new BufferedInputStream(fis);
            fos=new FileOutputStream("D:\\copy舞动的排序算法 冒泡排序1.flv");
            bos=new BufferedOutputStream(fos);
            byte[] b=new byte[1024*1];
            int len=0;
            while((len=bis.read(b))!=-1) {
                bos.write(b, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }   
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值