例子
以下示例显示java.io.ByteArrayOutputStream.reset()方法的用法。
package com.jc2182;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteArrayOutputStreamDemo {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream baos = null;
try {
String str = "";
// create new ByteArrayOutputStream
baos = new ByteArrayOutputStream();
// writing byte to output stream
baos.write(75);
// output stream to string
str = baos.toString();
System.out.println("Before Resetting : "+str);
// reset() method invocation
baos.reset();
// writing byte to output stream
baos.write(65);
// output stream to string()
str = baos.toString();
System.out.println("After Resetting : "+ str);
} catch(Exception e) {
// if I/O error occurs
e.printStackTrace();
} finally {
if(baos!=null)
baos.close();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
Before Resetting : K
After Resetting : A