• 文件输入流


    不管是字节流或者字符流,在java中他们使用的过程都很相似。对于一个输入流:

    1. 首先是创建一个与数据源相关的对象。如果数据源是硬盘上的文件,一个FileInputStream对象应当与此对应;
    2. 之后使用该对象的方法read()从哪个流中读取信息,即返回读取一个字节;
    3. 完成了从流中读取信息之后,调用close()方法关闭对流的使用。

    文件输入流可以使用构造方法FileInputStream(String)创建

    下面的语句创建了一个文件输入流:

    FileInputStream fis = new FileInputStream("scores.dat");

    下面的语句使用while循环来读取文件输入流对象df中的数据:

    int newBytes = 0;
    while (newBytes != -1)
    {
     newBytes = df.read();
    System.out.print(newBytes + " ");   
    }

    以字节方式读取源程序对应的类文件,并显示出来。

    //ReadBytes.java
    import java.io.*;
    public class ReadBytes {
    public static void main(String[] args) {
        try{//create file input stream file
            FileInputStream file = new FileInputStream("ReadBytes.class");
            boolean eof = false;
            int count = 0;
            while(!eof)//file not end 
            {
                int input = file.read();//read one character
                System.out.print(input + " ");//output the character
                if(input == -1)
                    eof = true;
                else    count++;
            }
            file.close();
            System.out.print("
     Bytes read:" + count);//output the count
        } catch(IOException e) {
            System.out.println("Error -- " + e.toString());
        }
    }
    }
  • 相关阅读:
    Centos 下oracle 11g 安装部署及手动建库过程
    MongoDB 存储引擎Wiredtiger原理剖析
    有关RDS上只读实例延时分析-同适用于自建MySQL主从延时分析判断
    windows 下my.ini的配置优化
    什么是purge操作
    linux内核调优参考
    通过第三方镜像仓库代理下载镜像
    微积分拾遗——链式法则
    Java中的RASP实现
    机器学习是什么
  • 原文地址:https://www.cnblogs.com/gride-glory/p/7719075.html
Copyright © 2020-2023  润新知