Java用OutputStream向接收者写入内容

OutputStream是java中的抽象类,它是所有表示字节输出流的所有类的超类,输出流介绍字节并将它发送到某个接收器。

抽象类OutputStream的主要方法有:

方法                       解释
close()                   关闭此输出流并释放相应资源
flush()                   刷新此输出流并强制写出所有缓冲的输出字节。
write(byte[] bytearry)             将字节写入此输出流。
write(byte[] bytearry,int offset,int len)    将指定的若干字节写入此输出流。将指定byte数组中从偏移量offset开始的len个字节
write(int)                将指定若干字节写入此输出流。

把字符串里面的数据写入文件中:

package test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IOtest {
	public static void main(String[] args) {
		readStrtoFile();
		//testByteArray();
	}
	
	/* 
	 * 把字符串写进文件。 
	 */  
	public static void readStrtoFile() {  
        if(!new File("d:/testROOT/").isDirectory()){
        	new File("d:/testROOT/").mkdirs();//目录不存在就新建目录
        }
        File f = new File("d:/testROOT/test.txt");
        if(!f.exists()){//文件不存在新建文件
        	try {
				f.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        OutputStream out=null;
		try {
			out = new FileOutputStream(f,true);//参数true表示不清空文件,在原来的基础上加新内容
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}  
        String str = "HelloWorld"; 
        byte[]b = str.getBytes(); 
        System.out.println(b.length);
        try {
			out.write(b);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}  
	}
}

FileOutputStream用通常的构造方法创造的对象,对文件进行写操作会覆盖文件中原有数据。如果想在文件的原有数据后追加新数据需要在构造方法中添加另一个参数。示例代码如下:

FileOutputStream(File file,boolean append);//通过给定的file对象创建输出流对象

FileOutputStream(String filename,boolean append);//通过路径名称(相对或绝对)创建输出流对象

上面两个构造方法中,第二个参数传入true,那么通过FOS对象写出的数据都是文件末尾追加的。

在上面的程序中把"HelloWorld"写进test.txt这个文件中,且在控制台输出:

10

输出10是因为虽然每个字符(char)的字节长度为2,但是在String类型中,每个字符的字节长度为1.


ByteArrayOutputStream该类实现了一个输出流,其数据被写入由byte数组充当的缓冲区,缓冲区会随着数据的不断写入而自动增长。

测试例子:

    static public void testByteArray(){
    /* 创建一个新的byte数组输出流,参数指定缓冲区大小(以字节为单位),若使用无参构造器,缓冲区默认大小是32字节。*/
        ByteArrayOutputStream out=new ByteArrayOutputStream(100);
    /* size()方法返回此输出流当前有效字节的个数。*/
        System.out.println("Now the byte size of buffer:"+out.size());
    /* toString()方法将缓冲区内容转换为字符串。*/
        System.out.println("Now the content of the out: "+out.toString());
        byte c=97;
    /* 将指定字节写入此输出流。*/
        out.write(c);
        System.out.println("Now the byte size of buffer:"+out.size());
        System.out.println("Now the content of the out: "+out.toString());
    /* reset()方法将输出流的有效字节个数重新置为0,意味着丢弃输出流目前积累的所有输出。重新使用已分配的缓冲区空间。*/
        out.reset();
        System.out.println("Now the byte size of buffer:"+out.size());
        System.out.println("Now the content of the out: "+out.toString());
    }

输出:

Now the byte size of buffer:0
Now the content of the out: 
Now the byte size of buffer:1
Now the content of the out: a
Now the byte size of buffer:0
Now the content of the out: 

byte c=97;97的二进制数转成ascii码是a;所以输出流的值为a。

ObjectOutputStream、ObjectInputStream经常会和FileInputStream、FileOutputStream一起用,ObjectOutputStream用于将对象序列化写入文件,ObjectInputStream将对象反序列化读出(当然对象必须是实现了序列化的)。

    public void testObject() {
        Student s1=new Student("zdd1", 1);
        Student s2=new Student("zdd2", 2);
        FileOutputStream fout=new FileOutputStream("student.txt");
        /* ObjectOutputStream的参数可以是任何继承于OutputStream的子类,即写入任何指定的OutputStream都可以,ObjectInputStream也是如此。*/
        ObjectOutputStream oout=new ObjectOutputStream(fout);
        oout.writeObject(s1);
        oout.writeObject(s2);
        FileInputStream fin=new FileInputStream("student.txt");
        ObjectInputStream oin=new ObjectInputStream(fin);
        Student s3,s4;
        s3=(Student) oin.readObject();
        s4=(Student) oin.readObject();
        System.out.println("s3's info: "+s3.name+" "+s3.id+"\n"+"s4's info: "+s4.name+" "+s4.id);
    }

输出:

s3's info: zdd1 1
s4's info: zdd2 2

实现序列化的类可以以ObjectOutputStream流的形式在文件中传输。在student.txt中保存的值为:

aced 0005 7372 000c 7465 7374 2e53 7475
6465 6e74 a736 1471 194a 8897 0200 0249
0002 6964 4c00 046e 616d 6574 0012 4c6a
6176 612f 6c61 6e67 2f53 7472 696e 673b
7870 0000 0001 7400 047a 6464 3173 7100
7e00 0000 0000 0274 0004 7a64 6432 
这是把对象进行流化的结果。

猜你喜欢

转载自blog.csdn.net/deronn/article/details/80361427