服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

几种常用输出输出流的使用

// : c12:iostreamdemo.java
// typical i/o stream configurations.

// {runbyhand}
// {clean: iodemo.out,data.txt,rtest.dat}
// from 'thinking in java, 3rd ed.' (c) bruce eckel 2002
// www.bruceeckel.com. see copyright notice in copyright.txt.

import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.bufferedreader;
import java.io.bufferedwriter;
import java.io.bytearrayinputstream;
import java.io.datainputstream;
import java.io.dataoutputstream;
import java.io.eofexception;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.filereader;
import java.io.filewriter;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.io.randomaccessfile;
import java.io.stringreader;

public class iostreamdemo {
  // throw exceptions to console:
  public static void main(string[] argsthrows ioexception {
    // 1. reading input by lines:
    bufferedreader in = new bufferedreader(new filereader(
        "iostreamdemo.java"));
    string s, s2 = new string();
    while ((s = in.readline()) != null)
      s2 += s + "/n";
    in.close();

    // 1b. reading standard input:
    bufferedreader stdin = new bufferedreader(new inputstreamreader(
        system.in));
    system.out.print("enter a line:");
    system.out.println(stdin.readline());

    // 2. input from memory
    stringreader in2 = new stringreader(s2);
    int c;
    while ((c = in2.read()) != -1)
      system.out.print((charc);

    // 3. formatted memory input
    try {
      datainputstream in3 = new datainputstream(new bytearrayinputstream(
          s2.getbytes()));
      while (true)
        system.out.print((charin3.readbyte());
    catch (eofexception e) {
      system.err.println("end of stream");
    }

    // 4. file output
    try {
      bufferedreader in4 = new bufferedreader(new stringreader(s2));
      printwriter out1 = new printwriter(new bufferedwriter(
          new filewriter("iodemo.out")));
      int linecount = 1;
      while ((s = in4.readline()) != null)
        out1.println(linecount++ + ": " + s);
      out1.close();
    catch (eofexception e) {
      system.err.println("end of stream");
    }

    // 5. storing & recovering data
    try {
      dataoutputstream out2 = new dataoutputstream(
          new bufferedoutputstream(new fileoutputstream("data.txt")));
      out2.writedouble(3.14159);
      out2.writeutf("that was pi");
      out2.writedouble(1.41413);
      out2.writeutf("square root of 2");
      out2.close();
      datainputstream in5 = new datainputstream(new bufferedinputstream(
          new fileinputstream("data.txt")));
      // must use datainputstream for data:
      system.out.println(in5.readdouble());
      // only readutf() will recover the
      // java-utf string properly:
      system.out.println(in5.readutf());
      // read the following double and string:
      system.out.println(in5.readdouble());
      system.out.println(in5.readutf());
    catch (eofexception e) {
      throw new runtimeexception(e);
    }

    // 6. reading/writing random access files
    randomaccessfile rf = new randomaccessfile("rtest.dat""rw");
    for (int i = 0; i < 10; i++)
      rf.writedouble(i * 1.414);
    rf.close();
    rf = new randomaccessfile("rtest.dat""rw");
    rf.seek(8);
    rf.writedouble(47.0001);
    rf.close();
    rf = new randomaccessfile("rtest.dat""r");
    for (int i = 0; i < 10; i++)
      system.out.println("value " + i + ": " + rf.readdouble());
    rf.close();
  }
///:~

扫描关注微信公众号