1. Java中I/O的OutputStream流
class="java" name="code">
1. 使用OutputStream流输出文件
OutputStream os = new FileOutputStream("c:\\out.txt", true);
true时在后面追加;
false时在开头重写。
import java.io.FileOutputStream;
import java.io.OutputStream;
public class OutputStreamTest1 {
public static void main(String[] args) throws Exception{
OutputStream os = new FileOutputStream("c:\\out.txt", true);
String str = "aaaaa";
byte[] buffer = str.getBytes();
os.write(buffer);
os.close();
}
}
2. 使用BufferedOutputStream流
如果不关闭流,数据就不会写到文件中。(文件会保存到项目根目录中)
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class BufferedOutputStreamTest1{
public static void main(String[] args) throws Exception{
OutputStream os = new FileOutputStream("1.txt");
BufferedOutputStream bos = new BufferedOutputStream(os);
bos.write("http://www.google.com".getBytes());
bos.close();
os.close();
}
}