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 a string to a byte array
Out.write (b);
Out.close ();//Not closed stream
}
}
Result:zhangsan
Although the byte stream is not closed, 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 a string to a byte array
Writer.write (b);
Writer.close ();//Not closed stream
}
}
Result
//When the program runs, nothing is found because the character stream operation uses a buffer, and when the character stream is closed, the contents of the buffer are forced to be output, but the contents of the buffer cannot be output if the program is not closed .
What is the case analysis in Java caused by using byte stream and character stream not to close the flow?