集册 Java实例教程 从文件创建缓冲的输入流,并读回字节

从文件创建缓冲的输入流,并读回字节

欢马劈雪     最近更新时间:2020-01-02 10:19:05

528
从文件创建缓冲的输入流,并读回字节
/* 来 自 N o w J a v a . c o m*/

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;


public class Main {

  public static void main(String[] arguments) {

    boolean success = writeStream();

    System.out.println("\nReading: ");

    boolean readSuccess = readStream();

  }

  static boolean writeStream() {

    try (FileOutputStream file = new FileOutputStream("numbers.dat");

        BufferedOutputStream buff = new BufferedOutputStream(file)) {
//NowJava.com

      for (int out = 0; out <= 200; out++) {

        buff.write(out);

        System.out.print(" " + out);

      }

      buff.close();

      return true;

    } catch (IOException e) {

      System.out.println("Exception: " + e.getMessage());

      return false;

    }

  }


  static boolean readStream() {

    try (FileInputStream file = new FileInputStream("numbers.dat");

        BufferedInputStream buff = new BufferedInputStream(file)) {

      int in;

      do {

        in = buff.read();

        if (in != -1)

          
展开阅读全文