?? fileoutputstreamdemo.java
字號:
package fileoutputstream;
/**
下面的例子創(chuàng)建一個樣本字節(jié)緩沖器。先生成一個String對象,接著用getBytes( )方法
提取字節(jié)數(shù)組對等體。然后創(chuàng)建了三個文件。第一個file1.txt將包括樣本中的各個字節(jié)。第
二個文件是file2.txt,它包括所有字節(jié)。第三個也是最后一個文件file3.txt,僅包含最后的四
分之一。不像FileInputStream類的方法,所有FileOutputStream類的方法都返回一個void類型
值。在出錯情況下,這些方法將引發(fā)IOException異常。
*/
// Demonstrate FileOutputStream.
import java.io.*;
class FileOutputStreamDemo {
public static void main(String args[]) throws Exception {
String source = "Now is the time for all good men\n"
+ " to come to the aid of their country\n"
+ " and pay their due taxes.";
byte buf[] = source.getBytes();
OutputStream f0 = new FileOutputStream("file1.txt");
for (int i = 0; i < buf.length; i += 2) {
f0.write(buf[i]);
}
f0.close();
OutputStream f1 = new FileOutputStream("file2.txt");
f1.write(buf);
f1.close();
OutputStream f2 = new FileOutputStream("file3.txt");
f2.write(buf, buf.length - buf.length / 4, buf.length / 4);
f2.close();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -