In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of byte input and output stream in Java IO, which is very detailed and has certain reference value. Friends who are interested must finish it!
It is about byte input and output stream: InputStream, OutputSteam (in the red rectangle below), and its typical implementation (FileInputSteam, FileOutStream) in the red oval box.
1. Byte output stream: OutputStreampublic abstract class OutputStream extends Object implements Closeable, Flushable
This abstract class is the superclass of all classes that represent the byte output stream. The output stream receives the output bytes and sends them to a receiver.
Method summary:
Let's introduce it with FileOutputStream, a typical implementation of byte output stream OutputStream:
/ / 1. Create the target object, and the output stream represents the file to which the data is saved. Do not write the drive letter. By default, the file is File target = new File ("io" + File.separator+ "a.txt") in the root directory of the project; / / 2. Create the byte output stream object of the file. The second parameter is Boolean. True means that the file written later is appended to the data, and false means to overwrite OutputStream out = new FileOutputStream (target,true). / / 3. Specific IO operation (write data to file a.txt) / * void write (int b): write a byte to file * void write (byte [] b): write all bytes in array b to file * void write (byte [] bjinint off) Int len): writes the len bytes from the off index in the array b to the file * / out.write (65) / / write A to the file out.write ("Aa" .getBytes ()); / / write the Aa to the file out.write ("ABCDEFG" .getBytes (), 1,5); / / write the BCDEF to the file / / after the above operation, the data in the a.txt file is AAaBCDEF / / 4, close the stream resource out.close () System.out.println (target.getAbsolutePath ()); 2. Byte input stream: InputStreampublic abstract class InputStream extends Object implements Closeable
This abstract class is the superclass of all classes that represent the input byte stream.
Method summary:
Let's introduce it with FileInputStream, a typical implementation of byte output stream InputStream:
/ / 1. Create a target object, and the input stream indicates that the data of that file is saved to the program. Do not write the drive letter. By default, the file is saved in the root directory of the project / / a.txt: AAaBCDEF File target = new File ("io" + File.separator+ "a.txt"); / / 2. Create the input stream object InputStream in = new FileInputStream (target). / / 3. Specific IO operation (reading the data in the a.txt file into the program) / * Note: read the data in the file and return-1 * int read (): read a byte when there is no data in the end. Return read bytes * int read (byte [] b): read multiple bytes and save them to array b, store them from the position where array b has index 0, return several bytes read * int read (byte [] b byte int off,int len): read multiple bytes and store them in array b, starting at the position where array b has index 0 Length is len bytes * / int read (): reads a byte and returns the read byte int data1 = in.read () / / get the first byte of data in a.txt file System.out.println ((char) data1); / / A / / int read (byte [] b): read multiple bytes and save them in array b byte [] buffer = new byte [10]; in.read (buffer) / / get the first 10 bytes in the a.txt file and store them in the buffer array System.out.println (Arrays.toString (buffer)); / / [65, 97, 66, 67, 68, 69, 70, 0, 0, 0] System.out.println (new String (buffer)) / / AaBCDEF [] / / int read (byte [] b off,int len): read multiple bytes and store them in array b, starting from index off to len in.read (buffer, 0,3); System.out.println (Arrays.toString (buffer)); / / [65,97,66,0,0,0,0,0,0,0,0] System.out.println (new String (buffer)) / / AaB [] / / 4. Close the stream resource in.close (); 3. Copy the file with byte stream / * copy the a.txt file into b.txt * / 1, create the source and destination File srcFile = new File ("io" + File.separator+ "a.txt") File descFile = new File ("io" + File.separator+ "b.txt"); / / 2, create input and output stream objects InputStream in = new FileInputStream (srcFile); OutputStream out = new FileOutputStream (descFile); / / 3, read and write operations byte [] buffer = new byte [10]; / / create a byte array with a capacity of 10 to store read data int len =-1 / / indicates how many bytes have been read. If-1, the end of the file while ((len=in.read (buffer))! =-1) {/ / print the read data System.out.println (new String (buffer,0,len)) / / read the len-length data from the buffer array to the b.txt file out.write (buffer, 0, len);} / / 4, close the stream resource out.close (); in.close (). The above is all the contents of the article "sample Analysis of Byte input and output streams in Java IO". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.