Package COM.HEPHEC;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.OutputStream;
public class outputstreamtest{
public static void Main (string[] args) throws exception{
OutputStream out=new FileOutputStream (New File ("E:" +file.separator+ "test.txt"));
String str= "Zhangsan";
Byte[] B=str.getbytes ()//convert string to byte array
Out.write (b);
Out.close ()//not closed stream
}
}
Result:zhangsan
Although the byte stream is not turned off, there is still content output in the file, which proves that the byte stream is directly manipulating the file itself.
Package COM.HEPHEC;
Import Java.io.File;
Import Java.io.FileWriter;
Import Java.io.Writer;
public class outputstreamtest{
public static void Main (string[] args) throws exception{
Writer writer=new FileWriter (New File ("E:" +file.separator+ "test.txt"));
String str= "Zhangsan";
Byte[] B=str.getbytes ()//convert string to byte array
Writer.write (b);
Writer.close ()//not closed stream
}
}
Result
When the program runs, it finds nothing, because the character stream operation uses a buffer and the contents of the buffer are forced to be exported when the character stream is closed, but the contents of the buffer are not output when the program is not closed.