java之 ------ 文件的输入、输出(一)
2017-07-19 08:37
375 查看
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异常。 */
相关文章推荐
- java输入输出处理-文件file
- 文件的输入输出与假加密——略析JAVA的IO流
- 黑马程序员-JAVA高级视频_IO输入与输出-20天-4(File文件写入)
- 黑马程序员-JAVA高级视频_IO输入与输出-20天-2(File文件列表)
- java中文件的输入输出
- Java输入输出之字节流拷贝文件
- Java里面最简单好用的文件输入输出方法
- java操作文件的输入输出
- Java输入输出(7)---向指定文件,指定位置插入内容
- 黑马程序员-JAVA高级视频_IO输入与输出-20天-9(切割合并文件)
- Java程序将输出到控制台的语句存入文件和windows DOS将文字输入到文件中
- java 输入/输出流小记,文件copy的例子
- 【Java】打印流与缓冲区读者完成输入与输出到文件操作
- Head First Java(4)—— 序列化和文件的输入/输出
- 黑马程序员-JAVA高级视频_IO输入与输出-18天-4(文件的续写)
- 黑马程序员-JAVA高级视频_IO输入与输出-20天-6(File文件删除)
- Android读写文件基于Java的文件输入输出流
- Java输入输出1文件复制
- java文件输入和输出
- 黑马程序员-JAVA高级视频_IO输入与输出-20天-3(File文件递归)