outputstream类是所有的字符输出类的父类,他是一个抽象类。

对于OutputStream类来说,其最基础的方法就是:write().

public abstract void write(int b) throws IOException

这个方法,写一个无符号字符(在0-255之间)

举例如下:

  1. import java.io.*;
  2. public class AsciiChart {
  3. public static void main(String[] args) {
  4. for (int i = 32; i < 127; i++) {
  5. System.out.write(i);
  6. // break line after every eight characters.
  7. if (i % 8 == 7) System.out.write('\n');
  8. else System.out.write('\t');
  9. }
  10. System.out.write('\n');
  11. }
  12. }

在这里,请注意输出:\n和\t。分别对应数字是:10和9,也可以这么写:

  1. import java.io.*;
  2. public class AsciiChart {
  3. public static void main(String[] args) {
  4. for (int i = 32; i < 127; i++) {
  5. System.out.write(i);
  6. // break line after every eight characters.
  7. if (i % 8 == 7) System.out.write(10);
  8. else System.out.write(9);
  9. }
  10. System.out.write('\n');
  11. }
  12. }

输出如下:

  1. % java AsciiChart
  2. ! " # $ % & '
  3. ( ) * + , - . /
  4. 0 1 2 3 4 5 6 7
  5. 8 9 : ; < = > ?
  6. @ A B C D E F G
  7. H I J K L M N O
  8. P Q R S T U V W
  9. X Y Z [ \ ] ^ _
  10. ` a b c d e f g
  11. h i j k l m n o
  12. p q r s t u v w
  13. x y z { | } ~
  14.  
  15. 而outputstream的这个抽象类具体定义如下:
  16.  
  17. /*
     * Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.
     * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     */
  18.  
  19. package java.io;
  20.  
  21. /**
     * This abstract class is the superclass of all classes representing
     * an output stream of bytes. An output stream accepts output bytes
     * and sends them to some sink.
     * <p>
     * Applications that need to define a subclass of
     * <code>OutputStream</code> must always provide at least a method
     * that writes one byte of output.
     *
     * @author  Arthur van Hoff
     * @see     java.io.BufferedOutputStream
     * @see     java.io.ByteArrayOutputStream
     * @see     java.io.DataOutputStream
     * @see     java.io.FilterOutputStream
     * @see     java.io.InputStream
     * @see     java.io.OutputStream#write(int)
     * @since   JDK1.0
     */
    public abstract class OutputStream implements Closeable, Flushable {
        /**
         * Writes the specified byte to this output stream. The general
         * contract for <code>write</code> is that one byte is written
         * to the output stream. The byte to be written is the eight
         * low-order bits of the argument <code>b</code>. The 24
         * high-order bits of <code>b</code> are ignored.
         * <p>
         * Subclasses of <code>OutputStream</code> must provide an
         * implementation for this method.
         *
         * @param      b   the <code>byte</code>.
         * @exception  IOException  if an I/O error occurs. In particular,
         *             an <code>IOException</code> may be thrown if the
         *             output stream has been closed.
         */
        public abstract void write(int b) throws IOException;
  22.  
  23.     /**
         * Writes <code>b.length</code> bytes from the specified byte array
         * to this output stream. The general contract for <code>write(b)</code>
         * is that it should have exactly the same effect as the call
         * <code>write(b, 0, b.length)</code>.
         *
         * @param      b   the data.
         * @exception  IOException  if an I/O error occurs.
         * @see        java.io.OutputStream#write(byte[], int, int)
         */
        public void write(byte b[]) throws IOException {
            write(b, 0, b.length);
        }
  24.  
  25.     /**
         * Writes <code>len</code> bytes from the specified byte array
         * starting at offset <code>off</code> to this output stream.
         * The general contract for <code>write(b, off, len)</code> is that
         * some of the bytes in the array <code>b</code> are written to the
         * output stream in order; element <code>b[off]</code> is the first
         * byte written and <code>b[off+len-1]</code> is the last byte written
         * by this operation.
         * <p>
         * The <code>write</code> method of <code>OutputStream</code> calls
         * the write method of one argument on each of the bytes to be
         * written out. Subclasses are encouraged to override this method and
         * provide a more efficient implementation.
         * <p>
         * If <code>b</code> is <code>null</code>, a
         * <code>NullPointerException</code> is thrown.
         * <p>
         * If <code>off</code> is negative, or <code>len</code> is negative, or
         * <code>off+len</code> is greater than the length of the array
         * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
         *
         * @param      b     the data.
         * @param      off   the start offset in the data.
         * @param      len   the number of bytes to write.
         * @exception  IOException  if an I/O error occurs. In particular,
         *             an <code>IOException</code> is thrown if the output
         *             stream is closed.
         */
        public void write(byte b[], int off, int len) throws IOException {
            if (b == null) {
                throw new NullPointerException();
            } else if ((off < 0) || (off > b.length) || (len < 0) ||
                       ((off + len) > b.length) || ((off + len) < 0)) {
                throw new IndexOutOfBoundsException();
            } else if (len == 0) {
                return;
            }
            for (int i = 0 ; i < len ; i++) {
                write(b[off + i]);
            }
        }
  26.  
  27.     /**
         * Flushes this output stream and forces any buffered output bytes
         * to be written out. The general contract of <code>flush</code> is
         * that calling it is an indication that, if any bytes previously
         * written have been buffered by the implementation of the output
         * stream, such bytes should immediately be written to their
         * intended destination.
         * <p>
         * If the intended destination of this stream is an abstraction provided by
         * the underlying operating system, for example a file, then flushing the
         * stream guarantees only that bytes previously written to the stream are
         * passed to the operating system for writing; it does not guarantee that
         * they are actually written to a physical device such as a disk drive.
         * <p>
         * The <code>flush</code> method of <code>OutputStream</code> does nothing.
         *
         * @exception  IOException  if an I/O error occurs.
         */
        public void flush() throws IOException {
        }
  28.  
  29.     /**
         * Closes this output stream and releases any system resources
         * associated with this stream. The general contract of <code>close</code>
         * is that it closes the output stream. A closed stream cannot perform
         * output operations and cannot be reopened.
         * <p>
         * The <code>close</code> method of <code>OutputStream</code> does nothing.
         *
         * @exception  IOException  if an I/O error occurs.
         */
        public void close() throws IOException {
        }
  30.  
  31. }
  32.  

java io读书笔记(5) Writing Bytes to Output Streams的更多相关文章

  1. java io读书笔记(6) Writing Arrays of Bytes

    显而易见,一次性写出一堆数据,要比一个byte一个byte的写,快多了,因此,outputstream,给出了2个增强型的write: public void write(byte[] data) t ...

  2. java io读书笔记(3)数值类型的数据

    input stream读取字节:out stream写入字节.Readers读取字符而Writers写入字符.因此,如果我们想理解input和output,我们首先就要明白 java如何处理字节,整 ...

  3. java io读书笔记(2)什么是stream

    什么是stream?stream就是一个长度不确定的有序字节序列. Input streams move bytes of data into a Java program from some gen ...

  4. java io读书笔记(1)综述

    学习,是要持之以恒的,再读一本书,坚持. Java™ I/O, 2nd Edition By Elliotte Rusty Harold ............................... ...

  5. java io读书笔记(8)FileInputStream/FileOutputStream的应用

    转自:http://www.cnblogs.com/jjtech/archive/2011/04/17/2019210.html 这是一对继承于InputStream和OutputStream的类,用 ...

  6. java io读书笔记(4)字符数据

    Number只是java程序中需要读出和写入的一种数据类型.很多java程序需要处理有一大堆的字符组成的text,因为计算机真正懂得的只有数字,因此,字符按照某种编码规则,和数字对应. 比如:在ASC ...

  7. java io读书笔记(7) Closing Output Streams

    输出完毕后,需要close这个stream,从而使操作系统释放相关的资源.举例: public void close( ) throws IOException 并不是所有的stream都需要clos ...

  8. java effective 读书笔记

    java effective 读书笔记 []创建和销毁对象 静态工厂方法 就是“封装了底层 暴露出一个访问接口 ” 门面模式 多参数时 用构建器,就是用个内部类 再让内部类提供构造好的对象 枚举 si ...

  9. Java IO学习笔记二

    Java IO学习笔记二 流的概念 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入输 ...

随机推荐

  1. 前端框架Bootstrap的Modal使用Ajax数据源,如何避免数据被缓存

    1.绑定事件,在关闭的时候,直接将数据清除: $("#model").on("hidden.bs.model",function(e){$(this).remo ...

  2. delphi 高版本可执行程序减小的办法

    选菜单里的 Project -> Options.. (Shift+Ctrl+F11)出现Project Options for Project1.exe窗口,在左边选 Packages出现如下 ...

  3. Python之 for循环\while循环

    list或tuple可以表示一个有序集合.如果我们想依次访问一个list中的每一个元素呢?比如 list: L = ['Adam', 'Lisa', 'Bart'] print L[0] print ...

  4. sql 语句查询练习题

    1. 查询Student表中的所有记录的Sname.Ssex和Class列. select sname,ssex,class from student 2. 查询教师所有的单位即不重复的Depart列 ...

  5. ubuntu下opencv在Qt中的使用

    1. 编译安装OpenCV2.4.9  本博已有文章描述 2. 安装Qt和QtCreator 从qt-project.org 下载Qt安装文件 qt-opensource-linux-x64-5.4. ...

  6. BLE-NRF51822教程-RSSI获取

    当手机和设备连接上后,设备端可以通过获取RSSI,在一定程度上判断手机离设备的相对距离的远近. 获取函数很简单直接调用sd_ble_gap_rssi_get 接口函数就行了,传入连接句柄和buff就能 ...

  7. selenium webdriver

    http://www.blogjava.net/qileilove/archive/2014/02/18/409975.html     Selenium VS Webdriver     Selen ...

  8. 002_base64的编码实现

    一.先贴一张ASCII码的图 二.再贴一张base64转换规则的图 二.python代码实现.

  9. windows下Qt5.1.0配置android环境搭建 good

    1.首先下载好需要配置的软件: 1>Qt 5.1.0 for Android (Windows 32-bit, 716 MB)(Info)下载地址: http://qt-project.org/ ...

  10. Qt 之 自定义提示信息框—迅雷风格(模拟QDialog类的exec()方法) good

    http://blog.csdn.net/goforwardtostep/article/details/53614830