码迷,mamicode.com
首页 > 其他好文 > 详细

OutPutStream

时间:2016-09-24 21:49:07      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/j-j-c-c/p/5904158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!