bytearrayoutputstreamdemo.java~2~
来自「java2参考大全上的例子的源码和自己的理解.」· JAVA~2~ 代码 · 共 39 行
JAVA~2~
39 行
package ytearrayoutputstream;
/**
public void reset()Resets the count field of this byte array output stream to zero,
so that all currently accumulated output in the ouput stream is discarded.
The output stream can be used again, reusing the already allocated buffer space.
*/
// Demonstrate ByteArrayOutputStream.
import java.io.*;
class ByteArrayOutputStreamDemo {
public static void main(String args[]) throws IOException {
ByteArrayOutputStream f = new ByteArrayOutputStream();
String s = "This should end up in the array";
byte buf[] = s.getBytes();
f.write(buf);
System.out.println("Buffer as a string");
System.out.println(f.toString());
System.out.println("Into array");
byte b[] = f.toByteArray();
for (int i = 0; i < b.length; i++) {
System.out.print( (char) b[i]);
}
System.out.println("\nTo an OutputStream()");
OutputStream f2 = new FileOutputStream("test.txt");
f.writeTo(f2);
f2.close();
System.out.println("Doing a reset");
f.reset();
for (int i = 0; i < 3; i++) {
f.write('X');
}
System.out.println(f.toString());
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?