A stream is a series of data.
When there is data interaction between different media, JAVA uses streams to implement it. The data source can be a file, a database, a network or even other programs.
For example, reading the data of a file into a program, from the perspective of the program, is called an input stream.
Byte stream (read and write data in bytes)
The InputStream byte input stream is also an abstract class, which only provides method declarations, but does not provide specific implementations of methods.
FileInputStream is a subclass of InputStream. The following is to read the file using FileInputStream as an example.
package testIO;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.Date;public class testFile {public static void main(String[] args) throws IOException {File f=new File("F:/test/lol.txt");//Create a file-based input stream FileInputStream fis=new FileInputStream(f);//Create a byte array, and its length is the length of the file byte[] bs=new byte[(int) f.length()];System.out.println("Read all contents of the file in the form of a byte stream: "+fis.read(bs)); for (byte b:bs){System.out.println(b);}fis.close();}}OutputStream byte output stream is also an abstract class, only providing method declarations, not providing specific implementations of methods.
FileOutputStream is a subclass of OutputStream. The following is to write data using FileOutputStream as an example.
package testIO;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class writeIO {public static void main(String[] args) throws IOException {File f=new File("F:/test/lol.txt");FileOutputStream fos=new FileOutputStream(f);byte data[]={87,88};fos.write(data);fos.close();}}Note: If the lol file does not exist, the write operation will automatically create the file. If the directory test does not exist, an exception will be thrown.
So if the directory test does not exist, how can the program be created automatically? If the path is F:/test/yang/lol.txt, and neither test nor yang exists?
package testIO;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class writeIO {public static void main(String[] args) throws IOException {File f=new File("F:/test/yang/csdn/lol.txt");System.out.println(f.exists());File dir=f.getParentFile();//Get the directory where the file is located if(!dir.exists()){dir.mkdirs();//If the file directory does not exist, the non-existent directory will be created}FileOutputStream fos=new FileOutputStream(f);byte data[]={87,88};fos.write(data);fos.close();}}The directory where the file is located is obtained in the program. If the directory does not exist, the directory will be created.
Summarize
The above is all the content of this article's detailed interpretation of Java IO streams, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!