//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 pat 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();
}
}
本文演示了如何使用Java的FileOutputStream类来写入不同形式的数据到三个不同的文本文件中,通过实例代码展示了基本的文件操作技巧。
343

被折叠的 条评论
为什么被折叠?



