java基础语法(二十八)IO-FileInputstream、FileOutputstream

FileInputstream

1、以下看会就好,不需要掌握

public class FileInputstreamTest01 {
    public static void main(String[] args) {
        FileInputStream fis=null;
        try {
            //创建文件字节输入流对象
            //文件路径:F:\java\test\IO\src\Demo\temp.txt (IDEA会自动把\变成\\,因为java中\表示转义)
            //FileInputStream fis = new FileInputStream("
            // ");
            //写成这个/也是可以的
             fis = new FileInputStream("F:/java/test/IO/src/Demo/temp.txt");
             //开始读
            int readData = fis.read();//这个方法的返回值是:读取到的"字节"本身。
            System.out.println(readData);//97
            readData=fis.read();
            System.out.println(readData);//98
            //已经读到文件的末尾了,再读的时候读取不到任何数据,返回-1.
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //在finally语句块当中确保流一定关闭
            if(fis != null){//避免空指针异常
                //关闭流的前提是:流不是空。流是null的时候没必要关闭。
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}

对以上程序改进,循环方式:

public class FileInputstreamTest02 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("F:\\java\\test\\IO\\src\\Demo\\temp.txt");
            /*while (true){
                int readData = fis.read();
                if(readData == -1){
                    break;
                }
                System.out.println(readData);
            }
             */

            //改造while循环
            int readData = 0;
            while ((readData = fis.read() )!= -1){
                System.out.println(readData);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

int read(byte[] b)
一次最多读取b.length个字节。
减少硬盘和内存的交互,提高程序的执行效率。
往byte[]数组当中读。

public class FileInputstreamTest03 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = null;
        try {
            //相对位置一定是从当前所在的位置作为起点开始找
            //IDEA默认的当前路径:工程Project的根就是IDEA的默认当前路径
            //fis=new FileInputStream("temp.txt");
            fis=new FileInputStream("IO/src/Demo/temp.txt");

            // 开始读,采用byte数组,一次读取多个字书。最多读取"数组.length"个字节。
            byte[] bytes = new byte[4]; //准备一个4个长度的byte数组,一次最多读取4个字节。
            //这个方法的返回值是:读取到的字节数量。( 不是字节本身)
            int readCount = fis.read(bytes);
            System.out.println(readCount); //第一次读到了4个字节。
            //将字节数组全部转换成字符串
            //System.out.println(new String(bytes));//abcd
            //不应该全部转换,应该是读取到多少个字节,转换多少个。
            System.out.println(new String(bytes,0,readCount));
            readCount = fis.read(bytes); //第二次只能读取到2个字节。
            System.out.println(readCount);
            //将字节数组全部转换成字符串
            //System.out.println(new String(bytes));//efcd
            System.out.println(new String(bytes,0,readCount));
            readCount = fis.read(bytes);//一个字节也没有读取到返回-1
            System.out.println(readCount);//-1


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2、 FileInputstream-最终版,需要掌握

public class FileInputStreamTest04 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("IO/src/Demo/temp2.txt");
            //准备一个byte数组
            byte[] bytes = new byte[4];
            /*while (true){
                int readCount=fis.read(bytes);
                if (readCount == -1){
                    break;
                }
                // 把byte数組转换成宇符申,读到多少个转换多少个。
                System.out.print(new String(bytes,0,readCount));
            }
             */
            int readCount = 0;
            while ((readCount=fis.read(bytes))!=-1){
                System.out.print(new String(bytes,0,readCount));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

3、FileInputStream类的其它常用方法:

int available() :返回流当中剩余的没有读到的字节数量
long skip(long n) :跳过几个字节不读。

public class FileInputStreamTest05 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("IO/src/Demo/temp.txt");
            System.out.println("总字节数量:"+fis.available());
            //读一个字节
            //int readByte = fis.read();
            //原来的文本是:abcdef 还剩下可以读的字节数量是5
           // System.out.println("剩下多少个字节没有读:"+fis.available());

            //这个方法有什么用?
            //byte[] bytes=new byte[fis.available()];//这种方式不太适合太大的文件,因为byte[]数组不能太大。
            //不需要循环了
            //直接读一次就行
            //int readCount = fis.read(bytes);
            //System.out.println(new String(bytes));

            //skip 跳过几个字节不读取
            fis.skip(3);
            System.out.println(fis.read());//100

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

FileOutputstream

文件字节输出流,负责写
从内存到硬盘

public class FileOutputStreamTest01 {
    public static void main(String[] args) {
        FileOutputStream fos=null;
        try {
            // myfile文件不存在的时候会自动新建!
            //这种方式谨慎使用,这种方式会先将原文件清空,然后重新写入。
            //fos=new FileOutputStream("myfile");
            //以追加的方式在文件末尾写入,不会清空原文件内容
            fos = new FileOutputStream("myfile",true);

            //开始写
            byte[] bytes={97,98,99,100};
            //将byte数组全部写出
            fos.write(bytes);//abcd
            //将byte数组的一部分出
            fos.write(bytes,0,2);//再写出ab
            //写完之后,最后一定要刷新
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值