This article mainly introduces the use of Java InputStream flow.
(1) FileInputStream: Subclass, read the data channel
Use steps:
1. Get target file: New file ()
2. Establish channel: New Fileinputstring ()
3. Read data: Read ()
4. Release resources: Close ()
Some packages that are to be imported by default
import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
The public static void main (string[] args) throws IOException {/
/TODO auto-generated the
method stub//call methods individually to view effects
t Est1 ();
System.out.println ("-------------------------------------------");
Test2 ();
System.out.println ("-------------------------------------------");
Test3 ();
System.out.println ("-------------------------------------------");
Test4 ();
}
(2) Three ways of reading data
1. Direct read (only one byte at a time)
int date = Fileinputstream.read ();
Char date3 = (char) fileinputstream.read ();
Mode one direct print public
static void Test1 () throws ioexception{
//(1) Gets the destination file path, filename
= new file ("C:\\users\\ Joke\\desktop\\demo1.java ");
(2) Establish the channel according to the target file path: New FileInputStream (file)
fileinputstream FileInputStream = new FileInputStream (file);
(3) reading data: Read ();
int date = Fileinputstream.read ()//here is int type
int date2 = Fileinputstream.read ();//
char date3 = (char) Fileinputstream.read (); Displays
System.out.println (date+ "\" +date2+ "\" +date3) in char type;
(4) Releasing Resources
Fileinputstream.close ();
}
2. Use for loops alone (low efficiency)
for (int i = 0; i < file.length (); i++) {
System.out.print ((char) fileinputstream.read ());
}
Mode two loops through the public
static void Test2 () throws ioexception{
//Pass time Test efficiency
long starttime = System.currenttimemillis ();
File File = new file ("C:\\users\\joke\\desktop\\demo1.java");
FileInputStream FileInputStream = new FileInputStream (file);
For loop for
(int i = 0; i < file.length (); i++) {
System.out.print ((char) fileinputstream.read ());
}
Fileinputstream.close ();
Long endtime = System.currenttimemillis ();
System.out.println ("Time spent reading the file:" + (Endtime-starttime));
}
3.byte[] Buffer (can only read the specified number of bytes cannot read a complete file)
byte[] bt = new byte[1024];
int count = Fileinputstream.read (BT);
System.out.println (New String (Bt,0,count));
Mode three creates a buffer (can only read the size of a set, cannot read a complete file) public
static void Test3 () throws ioexception{
File File = new file ("c:\\ Users\\joke\\desktop\\demo1.java ");
FileInputStream FileInputStream = new FileInputStream (file);
Creates a buffer, speeds up reading data, determines the byte size to read
byte[] bt = new byte[1024];
Read () reads byte
int count = Fileinputstream.read (BT);
System.out.println (count); Displays the number of bytes read
System.out.println (new String (Bt,0,count));//Convert bytes to string display
fileinputstream.close ();
}
4. Buffer and loop combination. The buffer is generally set to a multiple of 1024. Theoretically, the larger the buffer, the higher the Read efficiency
byte[] bt = new byte[1024];
int count = 0;
while (count = Fileinputstream.read (BT))!=-1) {
System.out.println (new String (Bt,0,count));
}
Mode four loops combined with buffer (high efficiency) public
static void Test4 () throws ioexception{
//Pass time Test efficiency
long starttime = System.currenttimemillis ();
File File = new file ("C:\\users\\joke\\desktop\\demo1.java");
FileInputStream FileInputStream = new FileInputStream (file);
The buffer is generally set to a multiple of 1024. Theoretically set the larger the buffer, the higher the Reading efficiency
byte[] bt = new byte[1024];
int count = 0;
Read returns-1, the proof has been traversed while
((count = Fileinputstream.read (BT))!=-1) {
//string display (start traversing the count length from the No. 0 byte in BT)
System.out.println (New String (Bt,0,count));
Fileinputstream.close ();
Long endtime = System.currenttimemillis ();
System.out.println ("Time spent reading the file:" + (Endtime-starttime));
}
Mo Mo said:
In the above, comparing the second and fourth methods, you will find that method four efficiency is relatively high, so the recommended use of the four methods
Here we are throwing the exception directly, we can use it in addition to throwing
try{ }cater{ }finally{ }
The way to handle exceptions
The above is a small series to introduce the Java IO Stream inputstring () the use of the input stream (), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!