java学习初探十七之IO流_FileInputStream

来源:互联网 发布:淘宝买家匿名提取软件 编辑:程序博客网 时间:2024/06/05 19:48

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();    }}
原创粉丝点击