OutputStream:

    FileOutputStream

    BufferedOutputStream  缓冲输出流

 package file;

 import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; public class Demo2 {
public static void main(String[] args) throws IOException {
// writeTest1();
// writeTest2();
writeTest3();
} //使用字节数组把数据写出
public static void writeTest3() throws IOException {
File file = new File("F:/2.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//数据写出
String data = "abcd";
byte[] buf = data.getBytes();
fileOutputStream.write(buf, 0, 2); //写出2个字节
fileOutputStream.close();
} //使用字节数组把数据写出
public static void writeTest2() throws IOException {
File file = new File("F:/2.txt");
//使用FileOutputStream(File),如果文件不存在,会自动创建目标文件。如果存在,则会先把目标文件的内容清空,再写内容
//使用FileOutputStream(File,true)构造函数,则会在末尾追加
FileOutputStream fileOutpubStream = new FileOutputStream(file);
//数据写出
String data = "hello world";
fileOutpubStream.write(data.getBytes());
//关闭资源
fileOutpubStream.close();
} //每次只能写一个字节数据
public static void writeTest1() throws IOException {
File file = new File("F:\\2.txt");
FileOutputStream fileOutpubStream = new FileOutputStream(file);
fileOutpubStream.write('h');
fileOutpubStream.write('e');
fileOutpubStream.write('l');
fileOutpubStream.write('l');
fileOutpubStream.write('o'); fileOutpubStream.close();
}
}
04-17 05:34