java io Stream is the input and output stream, the stream is a set of sequential, with the beginning and end of the byte combination, is the general name of the data transmission. That is, the transmission of data between two devices is called a stream, and the nature of the flow is data transmission. The IO stream can be divided into byte stream and character stream. Give the corresponding IO structure diagram: 650) this.width=650; "id=" aimg_293 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132147au70qijq0b0u5je0.png "class=" Zoom "width=" 570 "alt=" 132147au70qijq0b0u5je0.png "/> In the next period of time, will slowly introduce the use of various streams, this blog first introduce the fileoutputstream of the byte stream and the corresponding FileInputStream.
I. FileOutputStream (file output stream) OutputStream is an abstract class, and an abstract class must be implemented by subclasses. Now the output to the file is going to use FileOutputStream.
The FileOutputStream has four construction methods, respectively 1.FileOutputStream (file file)-------------writing data to files of file objects 2.FileOutputStream (file File,boolean append);------ Append Write data to file object 3.FileOutputStream (String path)-------------writing data to the specified file 4.FileOutputStream (String path,boolean append);-------- append Write data to the specified file When the value of append is true, data written to the file is appended to the original data, or the file's data is overwritten. The default is False.
Write Method 1: Byte writes
public static void Main (string[] args) {
try {
Create a file byte output stream object
OutputStream os=new FileOutputStream ("L:\\io.txt");
Data being written
String string= "Hello IO Stream";
Byte[]bytes=string.getbytes ();//Convert to byte array
for (int i=0;i<bytes.length;i++) {
Output to File
Os.write (Bytes[i]);
}
Os.close ();//Close stream
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
Copy Code method Two: Write all at once
public static void Main (string[] args) {
try {
Create a file byte output stream object
OutputStream os=new FileOutputStream ("L:\\io.txt", true);//Append
Data being written
String string= "Hello IO Stream";
Byte[]bytes=string.getbytes ();//Convert to byte array
Os.write (bytes);//Write All
Os.write (bytes,0,5) indicates a write length of 5 bytes starting from 0
Os.close ();//Close stream
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
Copy Code I. FileInputStream (file input stream) FileInputStream is the input byte obtained from a file in the system, with two construction methods 1.FileInputStream (File file) 2.FileInputStream (String Path)
Read Byte Method 1: Byte read
public static void Main (string[] args) {
try {
Create a byte input stream object
InputStream is=new FileInputStream ("L:\\io.txt");
int b=-1;
while ((B=is.read ())!=-1)//byte read, when 1 is complete
{
System.out.print ((char) b);//convert to character output
}
Is.close ();//Close stream
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
Copy Code The output is: Hello IO Stream. Such a one-byte read is very slow.
Read Byte Method 2: Read all bytes at once
public static void Main (string[] args) {
try {
File File=new file ("L:\\io.txt");
Create a byte input stream object
InputStream is=new fileinputstream (file);
To create a byte array based on the file size
Byte[]bytes=new byte[(int) file.length ()];
int Len=is.read (bytes);//Returns the length of the read byte
System.out.println ("read byte length:" +len);
SYSTEM.OUT.PRINTLN ("reads:" +new string (bytes));//build into string output
Is.close ();//Close stream
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
Copy Code Operation Result:650) this.width=650; "id=" aimg_294 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132250nrc1di5kpr2zi16d.png "class=" Zoom "width=" 349 "alt=" 132250nrc1di5kpr2zi16d.png "/>
The main disadvantage of this method of reading is to build a byte array as large as the file size, when the file is small, and when the file is large, memory may not be able to frame such a large array of bytes. Therefore, this method is only suitable for small files.
Read Byte Method 3: Read the specified length each time (the most common method)
public static void Main (string[] args) {
try {
Create a byte input stream object
InputStream is=new FileInputStream ("L:\\io.txt");
Specify the size of each read--can be modified based on performance bytes
Byte[]bytes=new Byte[8];
StringBuffer sb=new StringBuffer ();
int len=-1;//The actual length of each read
while ((Len=is.read (bytes))!=-1)
{
Sb.append (New String (Bytes,0,len));
}
System.out.println ("Read bytes:" +SB);
Is.close ();//Close stream
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
Copy Code Output Result: 650) this.width=650; "id=" aimg_295 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132320ku7uoivhohhz5h4h.png "class=" Zoom "width=" 336 "alt=" 132320ku7uoivhohhz5h4h.png "/> |