• java之 ------ 文件的输入、输出(一)


    import java.io.*;
    
    public class IntFile 
    {
        private String filename;
        
        public IntFile(String filename)
        {
            this.filename = filename;
        }
        
        public void writeToFile() throws IOException           //将Fibonacci序列值写入指定文件
        {
            FileOutputStream fout = new FileOutputStream(this.filename);
            DataOutputStream dout = new DataOutputStream(fout);
            short i=0,j=1;
            do
            {
                dout.writeInt(i);                              //向输出流写入一个整数
                dout.writeInt(j);
                i = (short)(i+j);
                j = (short)(i+j);
            } while (i>0);
            dout.close();                                      //先关闭数据流
            fout.close();                                      //再关闭文件流
        }
    
        public void readFromFile() throws IOException          //从指定文件里读取整数
        {
            FileInputStream fin = new FileInputStream(this.filename);
            DataInputStream din = new DataInputStream(fin);
            System.out.println(this.filename+":");
            while (true)                                       //输入流未结束时
                try
                {
                    int i = din.readInt(); //从输入流中读取一个整数
                    System.out.print(i+"  ");
                }
                catch (EOFException e)
                {
                    break;
                }
            din.close();                                       //先关闭数据流
            fin.close();                                       //再关闭文件流
        }
    
        public static void main(String args[]) throws IOException
        {
            IntFile afile = new IntFile("FibIntFile.dat");
            afile.writeToFile();
            afile.readFromFile();
        }
    }
    
    
    /*
    程序执行结果例如以下:
    FibIntFile.dat:
    0  1  1  2  3  5  8  13  21  34  55  89  144  233  377  610  987  1597  2584  4181  6765  10946  17711  28657  
    
    程序设计说明例如以下:
    1、readInt()方法到达输入流末尾时,抛出EOFException异常。
    
    */
    

  • 相关阅读:
    产生多种anchor的代码讲解!很好!
    一个报错
    将自己的数据制作成voc格式
    Python list 增加/插入元素的说明
    SAP 物料主数据屏幕增强 .
    软件測试必读书籍
    20160309,微软3月8日公布13个安全补丁
    UVA
    LLVM每日谈之十九 LLVM的第一本系统的书<Getting Started with LLVM Core Libraries>
    计算程序执行的时间
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7203812.html
Copyright © 2020-2023  润新知