22、java.io.OutputStream字节输出流

一、OutputStream

 

  1. 此抽象类是表示输出字节流的所有类的超类。
  2. 输出流接受输出字节并将这些字节发送到某个接收器。
  3. 需要定义 OutputStream 子类的应用程序必须始终提供至少一种可写入一个输出字节的方法。
  4. 直接已知子类:
  5.  ByteArrayOutputStream, FileOutputStream, FilterOutputStream, ObjectOutputStream, OutputStream, PipedOutputStream 
public abstract class OutputStream implements Closeable, Flushable
{
	/**
	 *关闭此输出流并释放与此流有关的所有系统资源。
	 *close 的常规协定是:该方法将关闭输出流。
	 *关闭的流不能执行输出操作,也不能重新打开。 
	 */
	public void close()
           throws IOException{}

	//刷新此输出流并强制写出所有缓冲的输出字节,OutputStream 的 flush 方法不执行任何操作
	public void flush()
           throws IOException{}

	//将 b.length 个字节从指定的 byte 数组写入此输出流。write(b) 的常规协定是:应该与调用 write(b, 0, b.length) 的效果完全相同
        //不用刷新,就直接写入数据了,和字符流不一样
	public void write(byte[] b)
           throws IOException{}

	//将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流
	public void write(byte[] b,
                  int off,
                  int len)
           throws IOException{}

	//将指定的字节写入此输出流。write 的常规协定是:向输出流写入一个字节。要写入的字节是参数 b 的八个低位。b 的 24 个高位将被忽略。 
	public abstract void write(int b)
                    throws IOException;
}

 

二、FileOutputStream

 

  1. 文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。
  2. 文件是否可用或能否可以被创建取决于基础平台。
  3. 特别是某些平台一次只允许一个 FileOutputStream(或其他文件写入对象)打开文件进行写入。
  4. 在这种情况下,如果所涉及的文件已经打开,则此类中的构造方法将失败。 
  5. FileOutputStream 用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用 FileWriter
public class FileOutputStream extends OutputStream
{
	//创建一个向指定 File 对象表示的文件中写入数据的文件输出流
	public FileOutputStream(File file)
                 throws FileNotFoundException{}

	//创建一个向指定 File 对象表示的文件中写入数据的文件输出流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处
	public FileOutputStream(File file,
                        boolean append)
                 throws FileNotFoundException{}

	//File file 可以换成 String name
}

 

三、InputStream

 

  1. 此抽象类是表示字节输入流的所有类的超类。
  2. 需要定义 InputStream 子类的应用程序必须总是提供返回下一个输入字节的方法。
  3. 直接已知子类
  4. InputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream 
  5. AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, 
public abstract class InputStream implements Closeable
{
	/**
	 *返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。
	 *下一个调用可能是同一个线程,也可能是另一个线程。
	 *一次读取或跳过此估计数个字节不会受阻塞,但读取或跳过的字节数可能小于该数。
	 *注意,有些 InputStream 的实现将返回流中的字节总数,但也有很多实现不会这样做。
	 *试图使用此方法的返回值分配缓冲区,以保存此流所有数据的做法是不正确的
	 */
	public int available()
              throws IOException{}

	/**
	 *从输入流中读取数据的下一个字节。
	 *返回 0 到 255 范围内的 int 字节值。
	 *如果因为已经到达流末尾而没有可用的字节,则返回值 -1。
	 *在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。 
	 */
	public abstract int read()
                  throws IOException;

	//从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
	public int read(byte[] b)
         throws IOException{}

	//将输入流中最多 len 个数据字节读入 byte 数组。
	public int read(byte[] b,
                int off,
                int len)
         throws IOException{}

	//跳过和丢弃此输入流中数据的 n 个字节
	public long skip(long n)
          throws IOException{}
}

 

 

四、FileInputStream

 

  1. FileInputStream 从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。 
  2. FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。
public class FileInputStream extends InputStream
{
	//通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定
	public FileInputStream(File file)
                throws FileNotFoundException{}

	//通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定
	public FileInputStream(String name)
                throws FileNotFoundException{}
}

 

五、示例

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 
 * 需求:拷贝任意文件
 *
 */
public class InputStreamTest {

	public static void main(String[] args) throws IOException {
		String resName = "E:/m.jpg";
		String desName = "c:/m.jpg";
		copy(resName,desName);
		
	}
	public static void copy(String resName,String desName) throws IOException
	{
		FileInputStream fis = new FileInputStream(resName);
		FileOutputStream fos = new FileOutputStream(desName);
		byte[] buf = new byte[1024];
		int num = 0;
		while((num=fis.read(buf))!=-1)
		{
			fos.write(buf);
		}
		fis.close();
		fos.close();
		System.out.println("复制成功");
	}

}

 

六、缓冲区字节流

 

/**
 *该类实现缓冲的输出流。
 *通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。 
 */
public class BufferedOutputStream extends FilterOutputStream
{
	//创建一个新的缓冲输出流,以将数据写入指定的底层输出流
	public BufferedOutputStream(OutputStream out){}

	//创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。
	public BufferedOutputStream(OutputStream out,
                            int size){}
}

 

/**
 *BufferedInputStream 为另一个输入流添加一些功能,
 *即缓冲输入以及支持 mark 和 reset 方法的能力。
 *在创建 BufferedInputStream 时,会创建一个内部缓冲区数组。
 *在读取或跳过流中的字节时,可根据需要从包含的输入流再次填充该内部缓冲区,一次填充多个字节。
 */
public class BufferedInputStream extends FilterInputStream
{
	//创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。创建一个内部缓冲区数组并将其存储在 buf 中。 
	public BufferedInputStream(InputStream in){}

	//创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。创建一个长度为 size 的内部缓冲区数组并将其存储在 buf 中
	public BufferedInputStream(InputStream in,
                           int size){}
}

 

七、示例

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class InputStreamReaderTest {

	public static void main(String[] args) throws IOException {
		BufferedReader br = 
				new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = 
				new BufferedWriter(new OutputStreamWriter(System.out));
		String line = null;
		while((line=br.readLine())!=null)
		{
			if(line.equals("over"))
				break;
			bw.write(line.toUpperCase());
			bw.newLine();
			bw.flush();
		}
		bw.close();
		br.close();
	}
}

 

八、PrintStream

 

  1. PrintStream 为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。
  2. 它还提供其他两项功能。
  3. 与其他输出流不同,PrintStream 永远不会抛出 IOException;
  4. 而是,异常情况仅设置可通过 checkError 方法测试的内部标志。
  5. 另外,为了自动刷新,可以创建一个 PrintStream;
  6. 这意味着可在写入 byte 数组之后自动调用 flush 方法,可调用其中一个 println 方法,或写入一个换行符或字节 ('\n')。 
  7. PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。在需要写入字符而不是写入字节的情况下,应该使用 PrintWriter 类。 
public class PrintStream extends FilterOutputStream implements Appendable, Closeable
{
	//创建具有指定文件且不带自动行刷新的新打印流
	public PrintStream(File file)
            throws FileNotFoundException{}

	//创建具有指定文件名称和字符集且不带自动行刷新的新打印流
	public PrintStream(File file,
                   String csn)
            throws FileNotFoundException,
                   UnsupportedEncodingException{}

	//创建新的打印流。此流将不会自动刷新
	public PrintStream(OutputStream out){}

	//创建新的打印流
	public PrintStream(OutputStream out,
                   boolean autoFlush){}

	//创建新的打印流
	public PrintStream(OutputStream out,
                   boolean autoFlush,
                   String encoding)
            throws UnsupportedEncodingException{}

	//创建具有指定文件名称且不带自动行刷新的新打印流
	public PrintStream(String fileName)
            throws FileNotFoundException{}

	//创建具有指定文件名称和字符集且不带自动行刷新的新打印流
	public PrintStream(String fileName,
                   String csn)
            throws FileNotFoundException,
                   UnsupportedEncodingException{}

	//打印 boolean,char,double,float,int,long,char[] s,Ojbect obj,String s 值
	public void print(boolean b){}
	public void print(boolean b){}//打印,然后终止该行
}

 

九、示例

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;

public class PrintStreamTest {

	public static void main(String[] args) throws IOException{
		//设置标准输入
		System.setIn(new FileInputStream("E:/m.txt"));
		//设置标准输出
		System.setOut(new PrintStream("C:/m.txt"));
		
		BufferedReader br = 
				new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw =
				new BufferedWriter(new OutputStreamWriter(System.out));
		
		String line = null;
		while((line=br.readLine())!=null)
		{
			bw.write(line);
			bw.newLine();
			bw.flush();
		}
		br.close();
		bw.close();
	}
}

 

import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * 将异常信息存入异常文件
 *
 */
public class PrintStreamTest {

	public static void main(String[] args) throws FileNotFoundException{
		try{
			int[] arr = new int[2];
			System.out.println(arr[2]);
		} catch(Exception e){
			System.setOut(new PrintStream("E:/exception.txt"));
			
			Date d = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String time = sdf.format(d);
			System.out.println(time);
			e.printStackTrace(System.out);
		}
	}
}

 

import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Properties;

/**
 * 
 * 将系统信息存入文件
 *
 */
public class PrintStreamTest {

	public static void main(String[] args) throws FileNotFoundException{
		Properties pro = System.getProperties();
		pro.list(new PrintStream("E:/properties.txt"));
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值