11th article-java IO
-
every sentence : Do not remember the advice of others when you fail, do not remember to cherish after losing
-
Beginner's experience : the more difficult the environment, the more intelligent
-
( Author: jeep/711) [Java Notes | time: 2017-04-17| JAVA IO Stream]
1. Flow concept
A stream is a series of flowing characters, a channel that sends information in FIFO mode.
That is, the transmission of data between the two devices is called a stream, the nature of the flow is data transmission, according to the data transmission characteristics of the stream abstracted into various classes, convenient and more intuitive data manipulation
2.JAVA IO Stream
Java IO Stream is the basis for the implementation of input/output, it is easy to implement data input/output operations
in Java, different input/output sources (keyboards, files, network connections, etc.) are abstracted as "streams" that allow Java programs to access different input/output sources in the same way. Stream is an ordered data from Origin (source) to receive (sink)
Java puts all traditional stream types (classes or abstract classes) in the java.io package for input/output functions
3.JAVA IO Stream classification
Categorized by Flow direction: can be divided into input stream and output stream:
-
Input stream: Data can only be read from, not written to
-
Output stream: You can only write data to it, and you cannot read from it
byte stream and character stream:
According to the role of the stream, can be divided into node flow and processing flow
four base classes for IO streams:
Java simplifies input/output processing by abstracting the ordered data from all devices into a stream model
Java's IO stream involves more than 40 classes, which look miscellaneous and messy, but are actually very regular and have very close links to each other .
More than 40 classes of the IO stream of Java are derived from 4 abstract base classes:
-
Inputstream/reader: The base class for all input streams, which is the input byte stream, which is the input character.
-
Outputstream/writer: The base class for all output streams, which is the output byte stream and the output character stream
-Input stream: InputStream and reader are the base classes of all the input streams, all of which are two abstract classes that themselves cannot create instances to perform input, but they will be so-called templates for all input streams, so their approach is to be used by all input streams
-
int read (): reads a single byte from the input stream (equivalent to removing a drop of water from the pipe shown in Figure 15.5), returning the read byte data (Byte data can be converted directly to the INT type)
-
int read (byte[]/char[] b): reads up to b.length bytes of data from the input stream and stores it in byte array B, returning the actual number of bytes read
-
int read (byte[]/char[] b, int off, int len): reads up to len bytes of data from the input stream and stores it in array B, when placed in the B array, not starting from the beginning of the array, but starting from the off position, returning the actual number of bytes read
-Output stream: OutputStream and writer are very similar, they use models to perform the output, and two streams provide the following three methods
-
void write (int c): Outputs the specified byte/character to the output stream, where c can represent either a byte or a character
-
void Write (byte[]/char[] buf): Outputs data from a byte array/character array to the specified output stream
-
void Write (char[] cbuf, int off, int len): Starts from the off position in a byte array/character array, and the byte/character length len is output to the output stream
Usage of the processing stream:
-
Using a process flow to wrap the node stream, the program performs the input/output function by processing the stream, allowing the node stream to interact with the underlying I/O device, file
-
It is very simple for a program to use a process flow, and it is usually only necessary to pass in a node stream as a constructor parameter when creating the process flow, which is the process flow that wraps the node stream
4. Flow Operation Law
-
1, clear source and purpose.
Data source: Is required to read, you can use two systems: InputStream, reader;
Data sinks: is required to write, you can use two systems: OutputStream, Writer;
-
2. Is the data of the operation plain text?
If it is: Data source: Reader
Data exchange: Writer
If not: Data source: InputStream
Data exchange: OutputStream
-
3, although a system has been identified, but there are too many objects in the system, in the end with Which?
Data devices that are explicitly operational.
Data source corresponding device: Hard disk (File), memory (array), keyboard (system.in)
Data sinks corresponding Devices: Hard disk (File), memory (array), console (System.out)
-
4. Do I need to attach other functions to the basic operation? Like cushioning, decorating if you need it.
5.java.io Bag
Read and write text files:
-
Read files with FileInputStream
-
Write files with FileOutputStream
-
Read a text file with BufferedReader
-
Write a text file with BufferedWriter
Read and write binary files:
-
DataOutputStream
-
DataInputStream
6. Byte stream file copy
1.public class iotest {2. public static void main (String[] args) throws IOException,InterruptedException {3. String path = "C:\\b.txt"; 4. readfile (Path);5. }6. private static void readfile (String path) {7. FileInputStream fis = null;8. try {9. fis = new fileinputstream (PATH);10. byte[] byt = new byte[1024];11. int len = fis.read (byt);12. system.out.println (new string (Byt, 0, len)); 13. } catch (ioexception e) {14. // Throw run-time exception 15. throw new RuntimeException (e);16. } finally {17. // put the Close method into finally to ensure that 18. will be executed. // determine if NULL pointer 19. if (Fis != null) {20. try {21. fis.close ();22. } catch (exception e) {23. throw new runtimeexception (e);24. }25. }26. }27. }28.}
1.public static void copyfile (String srcpath, string destpath) {2. FileInputStream fis = null;3. fileoutputstream fos = null;4. try {5. fis = new fileinputstream (SrcPath);6. fos = new fileoutputstream (DestPath); 7. byte[] byt = new byte[1024 * 1024];8. int len = 0;9. while ((Len = fis.read (byt)) ! = -1) {10. Fos.write (Byt, 0, len); 11. }12. } catch (ioexception e) {13. throw new runtimeexception (e);14. } finally {15. try {16. if (fis != null) {17. fis.close ();18. }19. } catch (ioexception e) {20. throw new runtimeexception (e);21. } finally {22. if (fos != null) {23. try {24. fos.close (); 25. } catch (ioexception e) {26. throw new RuntimeException (e);27. }28. }29. }30. }31. } 7. Character Stream file copy
1.public static void main (String[] args) throws Exception {2. String path1 = "C:/a.txt"; 3. string path2 = "C:/b.txt"; 4. copyfile2 (path1, path2);5. }6./**7. * copy files using character streams, with perfect exception handling 8. */9.public static void copyfile2 (string path1, string path2) {10. reader reader = null;11. writer writer = null;12. try {13. / / Open stream 14. reader = new FileReader (path1);15. writer = new FileWriter (path2); 16. // to copy 17. int ch = -1;18. while ((Ch = reader.read ()) != -1) {19. writer.write (CH);20. }21. } catch (exception e) {22. throw new runtimeexception (e);23. } finally {24. // Close the stream, note that you must be able to execute to the close () method, so put it in the finally code block 25. try {26. if (reader != nULL) {27. reader.close ();28. }29. } catch (Exception e) {30. throw new runtimeexception (e); 31. } finally { 32. try {33. if (writer != null) {34. writer.close ();35. }36. } catch (exception e) {37. throw new RuntimeException (e); 38. } 39. }40. }41. }Beginner (JAVA IO stream advanced stage) Difficulty: ★★★★★ hope that each article can provide readers with help and promotion, which is the original intention of every author Thank you for your reading welcome your message and suggestions
-
Facebook:jeep SevenEleven
-
Twitter: @JEEP7ll
-
Sina official Weibo: @JEEP-711
-
GitHub Blog: Https://github.com/jeep711/jeep711.github.io
-
Blog garden:http://www.cnblogs.com/jeep711/
-
w3c/blog:http://www.w3cschool.cn/jeep711blog/
-
csdn/blog:http://blog.csdn.net/jeep911
-
51cto/blog:http://jeep711.blog.51cto.com/
-
Code Cloud: Http://git.oschina.net/JEEP711/jeep711.github.io
-
Email: [email protected],[email protected]
This article is from the "JEEP711" blog, make sure to keep this source http://jeep711.blog.51cto.com/12970787/1955574
11th-java IO