java学习初探十七之IO流_FileInputStream

1.FileInputStream

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

public class StreamTest {
    public static void main(String[] args) {
        FileInputStream fileInputStream=null;
        try {
            //1.要读取某文件,先与这个文件创建一个“输入流”
            //文件路径

            //String filepath="temp01.txt";
            //String filepath="E:\\mystudy\\workspace\\webservice\\Stream\\src\\temp01.txt";
            String filepath="E:/mystudy/workspace/webservice/Stream/src/temp01.txt";
            fileInputStream=new FileInputStream(filepath);

            //2.读取文件
            int i1=fileInputStream.read();
            int i2=fileInputStream.read();
            System.out.println(i1);//97 //以字节方式读取
            System.out.println(i2);//98  如果已经读取到文件的末尾,则返回-1
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //为了保证流一定会释放,所以在finally语句块中执行
            if(fileInputStream!=null) {
                try {
                    //释放文件流
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
import java.io.FileInputStream;
//以下程序存在缺点,频繁访问磁盘,伤害磁盘,并且效率低
public class StreamTest02 {
    public static void main(String[] args) throws Exception{
        //1.创建流
        FileInputStream fileInputStream=new FileInputStream("E:/mystudy/workspace/webservice/Stream/src/temp01.txt");
        //2.开始读
        int temp=0;
        while ((temp=fileInputStream.read())!=-1) {
            System.out.println(temp);   
        }
        //3.关闭
        fileInputStream.close();
    }
}

2.int read(byte[] bytes)

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

/*
 * int read(byte[] bytes)
 * 读取之前在内存中准备一个byte数组,每次读取多个字节存储到byte数组中。
 * 一次读取多个字节,不是单个字节读取
 * 
 * 效率高
 */
public class SteamTest03 {
    public static void main(String[] args) throws IOException {
        //1.创建输入流
        FileInputStream file=new FileInputStream("E:/mystudy/workspace/webservice/Stream/src/temp01.txt");
        //2.开始读
        //准备一个byte数组
        byte[] bytes=new byte[3];//每次最多读取3个字节

        //该方法返回的int类型的值代表的是 这次读取的 多少个字节
        int i1=file.read(bytes);
        System.out.println(new String(bytes));//abc
        int i2=file.read(bytes);
        System.out.println(new String(bytes));//def
        int i3=file.read(bytes);
        System.out.println(new String(bytes,0,i3));//g
        int i4=file.read(bytes);
        System.out.println(i1);//3
        System.out.println(i2);//3
        System.out.println(i3);//1
        System.out.println(i4);//-1
        //3.关闭
        file.close();
    }
}

3.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;

//循环读取
public class StreamTest04 {
    public static void main(String[] args) throws IOException {
        FileInputStream file=new FileInputStream("E:/mystudy/workspace/webservice/Stream/src/temp01.txt");
        //循环读取
        byte[] bytes=new byte[1024];//每次读取1kB
        /*while (true) {
            int temp=file.read(bytes);
            if(temp==-1) break;
            System.out.print(new String(bytes,0,temp));

        }*/
        int temp=0;
        while ((temp=file.read(bytes))!=-1) {
            System.out.print(new String(bytes,0,temp));

        }
        file.close();
    }
}

4.skip available

import java.awt.geom.FlatteningPathIterator;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class StreamTest05 {
    public static void main(String[] args) throws IOException {
        //1.创建流
        FileInputStream file=new FileInputStream("E:/mystudy/workspace/webservice/Stream/src/temp01.txt");

        //int available();返回流中估计剩余字节数
        System.out.println(file.available());//7
        System.out.println(file.read());//97
        System.out.println(file.available());//6
        System.out.println(file.read());//98
        //跳过2个字节
        file.skip(2);
        System.out.println(file.read());//101
        //关闭
        file.close();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值