Java java.io.*; provides the relevant classes for streaming input and output of data, Io flow is an abstraction of input and output, and Java abstracts data from these different sources and targets into data streams. The benefit of unifying the input and output data as a stream is that the program does not have to be concerned with whether to read the file or the data in the network, but to treat it uniformly as an IO stream.
IO stream is divided into character stream and byte flow , the main difference
BYTE stream |
Processing in bytes (one byte at a time) |
Using Class OutputStream |
Character Stream |
Processing by character (one character at a time is two bytes) |
Using class writer |
In fact, the byte stream and character stream processing is very similar (that is, each processing unit different), a lot of books are also put together in terms of the two.
Okay, now start code!
The first is the byte operation:
The main classes used are
Public abstract class OutputStream extends Object implements Closeable, flushable
This class is abstract and cannot be instantiated, so it is instantiated with its subclasses through an upward transformation , because the next thing to demonstrate is the file so the subclass is
public class Filteroutputstream extends OutputStream
Code Demo
ImportJava.io.*; Public classjavaoutputstreamdemo{ Public Static voidMain (string[] args)throwsexception{File F=NewFile ("test.txt");//Get action fileOutputStream out =NULL;//Statement OutputStreamout =NewFileOutputStream (f);//instantiate an object through a subclass's upward transformationString Hello= "Hello world!!!";//prepare the data to be written to the file byteB[] = Hello.getbytes ();//Convert a string to a byte arrayOut.write (b); //Write FileOut.close (); //Close Operation }}
Test.txt is a program that does not need to be created in advance to create files first
Open Test.txt We have a chance to see the data we just wrote
Next is the character stream operation:
The main classes used are
Public abstract class Writer extends Object
Also we need to instantiate through its subclasses through an upward transformation
Public class FileWriter extends OutputStreamWriter
It may not be obvious to look at the inheritance tree
Java.lang.Object
+-java.io.writer
+-java.io.outputstreamwriter
+-java.io.filewriter
Code Demo
1 ImportJava.io.File;2 ImportJava.io.Writer;3 ImportJava.io.FileWriter;4 5 Public classjavawriterdemo{//Throw Exception6 Public Static voidMain (string[] args)throwsexception{7File f =NewFile ("test.txt");//Preparing Files8Writer out =NULL;//declaring writer9out =NewFileWriter (f);//instantiating an object through a subclassTen OneString Hello = "Hello world!!";//characters to prepare for writing AOut.write (hello);//Write File - -Out.close ();//Close Operation the } -}
Run to get results
All of the above are overwrite the file to write as if you want to append we can look at another way of constructing
FileWriter (String fileName, boolean append);
FileOutputStream (String name, Boolean append);
Just set the Append parameter to True when you instantiate it. There is no demonstration here.
Third, Io stream in Java, output stream