zoukankan      html  css  js  c++  java
  • FileInputStream输入字节流

    java.io.FileInputStream
        1 文件字节输入流,万能的,任何类型的文件都可以采用这个流来读。
        2 字节的方式,完成输入的操作,完成读的操作(是从硬盘到内存的一个过程。)
     
    FileInputStream流案例1:
    package com.javaSe.FileInputStream;
    
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    
    /*
    java.io.FileInputStream
        1 文件字节输入流,万能的,任何类型的文件都可以采用这个流来读。
        2 字节的方式,完成输入的操作,完成读的操作(是从硬盘到内存的一个过程。)
    */
    public class FileInputStreamTest01 {
        public static void main(String[] args) {
            FileInputStream fis = null;
            try {
                // 创建文件字节输入流对象
                // 文件路径:C:UsersxlWuDesktop学习IO流(IDEA会自动把变成\表示转义)
                // 以下都是采用了:绝对路径的方式。
                // FileInputStream fileInputStream = new FileInputStream("C:UsersxlWuDesktop学习IO流	emp.txt");
                // 写成这个/也是可以的。
                fis = new FileInputStream("C:/Users/xlWu/Desktop/学习/IO流/temp.txt");
                
                // 开始读
                int readDate = fis.read();// 这个方法返回到的是:读取到的“字节”本身。
                System.out.println(readDate);// 97
        
                readDate = fis.read();
                System.out.println(readDate);// 98
        
                readDate = fis.read();
                System.out.println(readDate);// 99
        
                readDate = fis.read();
                System.out.println(readDate);// 68
        
                readDate = fis.read();
                System.out.println(readDate);// 69
        
                readDate = fis.read();
                System.out.println(readDate);// 70
                
                // 已经读到文件的末尾了,在读的时候读取不到任何数据,返回-1
                
                readDate = fis.read();
                System.out.println(readDate);// -1
        
                readDate = fis.read();
                System.out.println(readDate);// -1
        
                readDate = fis.read();
                System.out.println(readDate);// -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();
                    }
                }
            }
        }
    }

    FileInputStream流案例2:

    package com.javaSe.FileInputStream;
    
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    
    public class FileInputStreamTest02 {
        public static void main(String[] args) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream("C:\Users\xlWu\Desktop\学习\IO流\temp.txt");
                
                /*while (true){
                    int readDate = fis.read();
                    if (readDate == -1) {
                        break;
                    }
                    System.out.println(readDate);
                }*/
                
                // 改造while循环
                int readDate = 0;
                while ((readDate = fis.read()) != -1){
                    System.out.println(readDate);
                }
                
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    FileInputStream案例3:

    package com.javaSe.FileInputStream;
    
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    
    /*
    int read(byte[] b)
        一次最多读取b.length 个字节。
        减少硬盘和内存的交互,提高程序执行效率。
        往byte[]数组当中读。
    */
    public class FileInputStreamTest03 {
        public static void main(String[] args) {
            FileInputStream fis = null;
            try {
                // 相对路径的话呢?相对路径一定是从当前所在的位置作为起点开始找!
                // IDEA默认当前路径是哪里?工程Project的根就是IDEA的默认当前路径。
                // fis = new FileInputStream("tempFile2");
                // fis = new FileInputStream("Stream/tempFile2");
                // fis = new FileInputStream("Stream/src//tempFile3");
                fis = new FileInputStream("Stream\src\com\javaSe\FileInputStream\tempFile4");
                
                // 开始读,采用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); // 2
                // 将字节数组全部转换成字符串
                // 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();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    FileInputStream案例4:

    package com.javaSe.FileInputStream;
    
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    
    /*
    最终版,需要掌握。
    */
    public class FileInputStreamTest04 {
        public static void main(String[] args) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream("Stream\src\tempFile3");
                
                // 准备一个byte数组
                byte[] bytes = new byte[4];
                /*while (true){
                    int readCount = fis.read(bytes);
                    if (readCount == -1){
                        break;
                    }
                    String s = new String(bytes,0,readCount);
                    System.out.print(s);
                }*/
                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();
                    }
                }
            }
        }
    }

    FileInputStream案例5:

    package com.javaSe.FileInputStream;
    
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    
    /*
    FileInputStream类的其他常用方法:
        int available():返回流当中剩余的没有读到的字节数量。
        long skip(long n):跳过几个字节不读。
    */
    public class FileInputStreamTest05 {
        public static void main(String[] args) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream("tempFile");
                System.out.println("总字节数量 = " + fis.available());
                /*// 读1个字节
                // int readByte = fis.read();
                // 还剩下几个字节:5
                // System.out.println("还剩下多少个字节没有读 = " + fis.available());
                // 这个方法有什么用?
                byte[] bytes = new byte[fis.available()]; // 这种方式不太适合太大的文件,因为byte[]数组不能太大。
                // 不需要循环了。
                // 直接读一次就行了。
                int readCount = fis.read(bytes);// 6
                System.out.println(new String(bytes));//abcdef*/
                
                // skip跳过几个字节不读取,这个方法可能以后会用
                fis.skip(3);
                System.out.println(fis.read());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
  • 相关阅读:
    进度条
    选项菜单和上下文菜单
    Activity总结练习
    OnClickListener接口
    按钮监听器
    《Shell脚本学习指南》书籍目录
    Fiddler---前端利器
    从零开始学Linux[二]:常用操作:用户组、进程、网络、ssh、定时任务、Screen命令
    grunt安装、配置、在webstrom中使用
    Linux安装vim失败的解决办法
  • 原文地址:https://www.cnblogs.com/xlwu/p/13466079.html
Copyright © 2011-2022 走看看