IO流之FileInputStream

IO流之FileInputStream

读文件

基本读文件

以下代码使用FileInputStream来读取硬盘中的文件数据:

package com.javalearn.io.primary;

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

public class TestForFileInputStream {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("D:\\javaProject\\file1");  // file1文件无扩展名,内含6个字符abcdef
            int data = fileInputStream.read();
            System.out.println(data);  // 输出结果为字符的ascll码
            data = fileInputStream.read();  // 默认有指针指向文件开头,每执行一次read,指针后移
            System.out.println(data);
            data = fileInputStream.read();
            System.out.println(data);
            data = fileInputStream.read();
            System.out.println(data);
            data = fileInputStream.read();
            System.out.println(data);
            data = fileInputStream.read();
            System.out.println(data);
            data = fileInputStream.read();  // 后移到最后没有字符时,返回-1
            System.out.println(data);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (fileInputStream != null) {  // 关闭流的前提:流非空,避免空指针异常
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

输出结果:

97
98
99
100
101
102
-1

用while循环读文件

以下代码使用FileInputStream+while循环来读文件:

package com.javalearn.io.primary2;

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

public class TestForFileInputStream {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("D:\\javaProject\\file1");
            while (true) {
                int readData = fileInputStream.read();
                if(readData == -1) {
                    break;
                }
                System.out.println(readData);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

输出结果:

97
98
99
100
101
102

优化while循环读文件

以下代码使用FileInputStream+while循环读文件,比上一个方法有优化:

package com.javalearn.io.primary2;

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

public class TestForFileInputStream {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("D:\\javaProject\\file1");
            // 以下为优化之处
            int readData = 0;
            while ((readData = fileInputStream.read()) != -1) {  // 赋值语句外不要忘记加括号,即(readData = fileInputStream.read()),表示readData被赋值后的值。
                System.out.println(readData);
            }
            // 以上为优化之处
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

输出结果:

97
98
99
100
101
102
  • 以上三个方法缺点:一次读取一个字节,内存和硬盘交互太频繁。

文件读入byte数组中

以下代码把文件读入到byte数组中,实现一次读取多个字节:

package com.javalearn.io.readbybyte;

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

public class TestForFileInputStream {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("file1");  //file1文件中有 abcdef 6个字符
            byte[] bytes = new byte[4];  // 一次最多读4个字符
            
            int readCount = fileInputStream.read(bytes);  // 返回值为读到了多少个字节
            System.out.println(readCount);  //读取到4个
            System.out.println(new String(bytes,0,readCount));  //读到了什么内容
            
            readCount = fileInputStream.read(bytes);  // 继续往后读,byte数组用尽后,从头开始覆盖
            System.out.println(readCount);  //读取到剩下的2个
            System.out.println(new String(bytes,0,readCount));
            
            readCount = fileInputStream.read(bytes);  // 继续往后读
            System.out.println(readCount);  // 没了,返回-1

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

输出结果:

4
abcd
2
ef
-1

优化 文件读入byte数组中

以下代码依然是文件读入byte数组中,不同的是,增加了while循环,直接读取文件全部内容:

package com.javalearn.io.bestfileinputstream;

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

public class TestForFileInputStream {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("file1");
            byte[] bytes = new byte[4];
            int count = 0;
            while ((count = fileInputStream.read(bytes))!= -1) {
                System.out.print(new String(bytes,0,count));  // 此处sout中的println需要改为print,这样才会保留文件内容的原始格式
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

输出结果:

abcdef

available方法

available方法用于返回剩余未读字节数量,可以用它来避免读文件时的while循环:

package com.javalearn.io.available;

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

public class TestForAvailable {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("file1");
            byte[] bytes = new byte[fileInputStream.available()];  // 预知文件大小,避免while循环,但不适用于大文件,因为数组不能太大
            int count = fileInputStream.read(bytes);  // 别忘了读取
            System.out.println(new String(bytes));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

输出结果:

abcdef

skip 方法

skip方法用于跳过几个字节不读:

package com.javalearn.io.skip;

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

public class TestForSkip {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("file1");  // file1存放 abcdef 6个字符
            fileInputStream.skip(3);  // 跳过3个字节
            int a = fileInputStream.read();
            System.out.println(a);  // d的ascll码
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream!= null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

输出结果:

100
posted on 2021-12-04 13:45  菜小疯  阅读(153)  评论(0编辑  收藏  举报