例子
以下示例显示java.io.DataOutputStream.size()方法的用法。
package com.jc2182;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = null;
DataOutputStream dos = null;
byte[] buf = {87,64,72,31,90};
try {
// create file output stream
fos = new FileOutputStream("c:/test.txt");
// create data output stream
dos = new DataOutputStream(fos);
int size = 0;
// for each byte in the buffer
for (byte b:buf) {
dos.write(b);
// current value of the counter written
size = dos.size();
// print
System.out.print("Size: "+size + "; ");
}
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
} finally {
// releases all system resources from the streams
if(fos!=null)
fos.close();
if(dos!=null)
dos.close();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
Size: 1; Size: 2; Size: 3; Size: 4; Size: 5;