English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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 file data into the program is called an input stream from the perspective of the program.
Byte stream (reading and writing data in byte form)
InputStream, a byte input stream, is also an abstract class that only provides method declarations and does not provide specific method implementations.
FileInputStream is a subclass of InputStream, and the following example will demonstrate file reading using FileInputStream.
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 an input stream based on the file. FileInputStream fis=new FileInputStream(f); //Create a byte array with a length equal to the file length. byte[] bs=new byte[(int) f.length()]; System.out.println("Reading all the content of the file in byte stream format:");+fis.read(bs)); for (byte b:bs){ System.out.println(b); } fis.close(); } }
OutputStream, a byte output stream, is also an abstract class that only provides method declarations and does not provide specific method implementations.
FileOutputStream is a subclass of OutputStream, and the following example will demonstrate data writing using FileOutputStream.
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 operation will automatically create the file. If the directory 'test' does not exist, an exception will be thrown.
Then how can the program automatically create a directory if 'test' does not exist? If the path is F:/test/yang/Is 'lol.txt' and 'test' and 'yang' not present?
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 of the file if(!dir.exists()){ dir.mkdirs(); //If the file directory does not exist, it will create the non-existing directory } FileOutputStream fos=new FileOutputStream(f); byte data[]={87,88}; fos.write(data); fos.close(); } }
Get the directory of the file in the program, if the directory does not exist, it will create the directory
Summary
This is the full detailed explanation of java IO stream in this article. I hope it will be helpful to everyone. Those who are interested can continue to read other related topics on this site, and welcome to leave a message if there are any shortcomings. Thank you for your support to this site!
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)