首页 > 代码库 > OutPutStream

OutPutStream

OutputStream类(java.io.OutputStream)

public abstract class OutputStream extends Object implements Closeable, Flushable

Closeable:表示所有需要关闭的资源接口

Flushable:表示刷新处理

构造方法:public OutputStream()

普通方法:

public abstract void write(int b)throws IOException

输出单个字节

public void write(byte[] b)throws IOException

输出整个字节数组的数据

public void write(byte[] b,int off,int len)throws IOException

输出部分字节数组的数据

public void close()throws IOException

关闭输出流

 

 

FileOutputStream类(java.io.FileOutputStream)

public class FileOutputStream extends OutputStream

构造方法:

public FileOutputStream(File file) throws FileNotFoundException

覆盖式输出文件(用File类)

public FileOutputStream(File file, boolean append) throws FileNotFoundException

追加式输出文件(用File类)

public FileOutputStream(String name) throws FileNotFoundException

覆盖式输出文件(用String类)

public FileOutputStream(String name, boolean append) throws FileNotFoundException

追加式输出文件(用String类)

 

package wiki.jjcc.test.ops;

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;

 

publicclass Test1 {

    publicstaticvoid main(String[] args) throws Exception {

        String s = File.separator;

        File file = new File("D:"+ s + "test" + s + "a.txt");

        if(!file.getParentFile().exists()){

            file.getParentFile().mkdirs();

        }

        String msg = "Hello World!\r\n";

        OutputStream out11 = new FileOutputStream(file);

        out11.write(msg.getBytes());

        OutputStream out12 = new FileOutputStream(file,true);

        out12.write(msg.getBytes());

        OutputStream out21 = new FileOutputStream("D:"+ s + "test" + s + "b.txt");

        out21.write(msg.getBytes());

        OutputStream out22 = new FileOutputStream("D:"+ s + "test" + s + "b.txt",true);

        out22.write(msg.getBytes());

    }

}

a.txt和b.txt原本不存在,自动生成。增加使用true参数,则在已经存在的文件尾追加。

 

OutPutStream