IO流

23 篇文章 0 订阅

读取文件数据

public static void main(String[] args) throws IOException {
        //demo1();
        FileInputStream fis = new FileInputStream("aaa.txt");   //创建一个文件输入流对象,并关联aaa.txt
        int b;                                                  //定义变量,记录每次读到的字节
        while((b = fis.read()) != -1) {                         //将每次读到的字节赋值给b并判断是否是-1
            System.out.println(b);                              //打印每一个字节
        }

        fis.close();                                            //关闭流释放资源
    }

    public static void demo1() throws FileNotFoundException, IOException {
        FileInputStream fis = new FileInputStream("aaa.txt");   //创建一个文件输入流对象,并关联aaa.txt
        int x = fis.read();                                     //每读取一次就向后移动一次
        System.out.println(x);
        int y = fis.read();
        System.out.println(y);
        int z = fis.read();
        System.out.println(z);
        int a = fis.read();
        System.out.println(a);
        int b = fis.read();
        System.out.println(b);
        fis.close();
    }

文件输出

public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("bbb.txt"); //如果没有bbb.txt,会创建出一个
        //fos.write(97);                        //虽然写出的是一个int数,但是在写出的时候会将前面的24个0去掉,所以写出的一个byte
        fos.write(98);
        fos.write(99);
        fos.close();
    }

将所有数据读入数组中

public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("致青春.mp3");
        FileOutputStream fos = new FileOutputStream("copy.mp3");
        byte[] arr = new byte[fis.available()];                 //根据文件大小做一个字节数组
        fis.read(arr);                                          //将文件上的所有字节读取到数组中
        fos.write(arr);                                         //将数组中的所有字节一次写到了文件上
        fis.close();
        fos.close();
    }

缓冲区

public static void demo1() throws FileNotFoundException, IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("致青春.mp3"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.mp3"));

        int b;
        while((b = bis.read()) != -1) {
            bos.write(b);
            //bos.flush();      //需要随时刷新缓冲区内容的时候,用flush
        }

        bis.close();
        bos.close();
    }

中文处理

package cn.itcast.io;

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

public class Demo8_Chinese {

    /**
     * @param args
     * @throws IOException 
     * 字节流只读中文是有弊端的,有可能会读到半个中文
     * 解决方法有2
     * 1,用字符流读(编码表+字节流)
     * 2,将文件上的所有字节一次读到内存中,在内存中将所有字节转换成对应的字符串
     * ByteArrayOutputStream
     * 
     * 字节流写中文
     * 在只写中文的时候必须转换成字节数组写出去
     * 
     * 字节流可以拷贝任意类型的数据,因为所有的数据都是一字节的形式存在的
     */
    public static void main(String[] args) throws IOException {
        //demo1();
        demo2();
        //demo3();
    }

    public static void demo3() throws FileNotFoundException, IOException {
        FileInputStream fis = new FileInputStream("aaa.txt");
        FileOutputStream fos = new FileOutputStream("bbb.txt");

        int b;
        while((b = fis.read()) != -1) {
            fos.write(b);
        }

        fis.close();
        fos.close();
    }

    public static void demo2() throws FileNotFoundException, IOException {
        FileOutputStream fos = new FileOutputStream("bbb.txt");
        fos.write("你好".getBytes());                             //字节流写出中文的时候要转换成字节数组
        fos.write("\r\n".getBytes());                           //写出回车换行
        fos.write("你好".getBytes());         
        fos.close();
    }

    public static void demo1() throws FileNotFoundException, IOException {
        FileInputStream fis = new FileInputStream("bbb.txt");   //创建文件输入流,关联bbb.txt
        byte[] arr = new byte[3];                               //创建字节数组
        int len;
        while((len = fis.read(arr)) != -1) {                    //先将字节读到数组中
            System.out.println(new String(arr,0,len));          //转换成字符串打印在控制台,转换的时候有可能乱码
        }
        fis.close();
    }

}

AutoCloseable

package cn.itcast.io;

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

public class Demo9_IOException {

    /**
     * @param args
     * @throws IOException 
     * 在try()中创建的流对象必须实现了AutoCloseable这个接口,如果实现了,在try后面的{}(读写代码)执行后就会自动调用
     * 流对象的close方法将流关掉
     */
    public static void main(String[] args) throws IOException {
        //demo1();
        try(
            FileInputStream fis = new FileInputStream("aaa.txt");
            FileOutputStream fos = new FileOutputStream("bbb.txt");
            MyClose mc = new MyClose();
        ){
            int b;
            while((b = fis.read()) != -1) {
                fos.write(b);
            }
        }

    }

    public static void demo1() throws FileNotFoundException, IOException {
        //标准的异常处理代码
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("aaa.txt");
            fos = new FileOutputStream("bbb.txt");
            int b;
            while((b = fis.read()) != -1) {
                fos.write(b);
            }
        } finally {
            try {
                if(fis != null)
                    fis.close();
            }finally {
                if(fos != null)
                    fos.close();
            }
        }
    }

}

class MyClose implements AutoCloseable {
    public void close() {
        System.out.println("我关了");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值