Basic Java IO knowledge (I)

Source: Internet
Author: User

Basic Java IO knowledge (I)
Java IO Overview

I/O is the core issue of human-computer interaction, Because I/O is the main channel for computers to acquire and exchange data.
The entire IO process is the source data ---> program ---> destination
IO Classification
Character-based I/O interfaces: Reader, Writer I/O interfaces based on byte operations: InputStream, and OutputStream I/O interfaces based on disk operations: File I/O interfaces based on network operations: Socket (not in java. under the io package, but the essence of socket is also io operations)

A program needs InputStream or Reader to read data from the data source, and needs OutputStream or Writer to write data to the Target media.

InputStream and Reader are associated with the data source, and OutputStream and writer are associated with the target media.

Understanding of input and output streams

Input stream: input to memory

Output stream: it can be understood as output from memory
IO class overview

Byte-based IO operation interface

Character-based IO operation interface

The data persistence and network transmission of character and byte conversion interfaces are performed in bytes. Therefore, conversion from character to byte or from byte to character is required.

InputStreamReader is a bridge between byte and character conversion. The encoding character set must be specified during the process from InputStream to Reader. StreamDecoder is the OutputStreamWriter class that implements the decoding from byte to character. StreamEncoder completes the encoding process from character to byte.

File Operation Demo

 

Package com. weixuan. test; import java. io. file;/*** to learn File-related operations, the File class can only access files and File metadata (the object's attribute information) * If You Want To Read and Write File Content, fileInputStream, FileOutputStream, or RandomAccessFile ** @ author Nicholas **/public class FileTest {public static void main (String [] args) {// 1. Before using File, must have a File object File = new file (D: \ CentOS-7.0-1406-x86_64-DVD.iso); // 2. Check whether the File exists boolean isExists = file. exists (); System. out. println (isExists); // 3. The file length is long length = file. length (); System. out. println (length); // 4. Rename or move the file boolean isSuccess = file. renameTo (new File (test2.iso); System. out. println (isSuccess); // 5. delete the file boolean isDelete = file. delete (); System. out. println (isDelete); // 6. check whether a path is a file or a directory boolean isDirectory = file. isDirectory (); System. out. println (isDirectory); // 7. Read the File list in the directory File file2 = new File (C: \ Users \ weixu_000 \ Pictures); String [] filename = file2.list (); for (String files: filename) System. out. println (files );}}

Package com. weixuan. test; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream;/*** use FileInputStream and FileOutputStream to operate files ** @ author Nicholas ** FileOutputStream can write byte streams to files (that is, from Memory output to files) * FileInputStream can read the file content (read from the file and write it to the memory) in the form of a byte stream */public class FileTest2 {private static OutputStream outputStream; priv Ate static InputStream inputStream; public static void main (String [] args) {try {// true indicates the append object, and false indicates overwriting outputStream = new FileOutputStream (D: \ javawork \ JavaIOTest \ fileoutputStream.txt, false); // byte [] c = {'h', 'E', 'l', 'l', 'O ', 'w', 'O', 'R', 'l', 'D'}; outputStream. write (HelloWorld. getBytes ();/*** when you write data to FileOutputStream, the data may be cached in the memory. * At a later time, for example, when only X pieces of data can be written at a time, or when FileOutputStream is disabled, the data is actually written to the disk. * When FileOutputStream is not closed and you want to ensure that the data written to FileOutputStream is written to the disk, * You can call the flush () method, this method ensures that all data written to FileOutputStream is written to the disk. */OutputStream. flush (); outputStream. close ();} catch (IOException e) {e. printStackTrace ();} // read the object's byte stream try {inputStream = new FileInputStream (D: \ javawork \ JavaIOTest \ fileoutputStream.txt); int data = inputStream. read (); while (data! =-1) {System. out. println (inputStream. read (); data = inputStream. read ();} inputStream. close ();} catch (IOException e) {e. printStackTrace ();}}}

Package com. weixuan. test; import java. io. fileReader; import java. io. fileWriter; import java. io. IOException; import java. io. reader; import java. io. writer;/*** FileReader and FileWriter ** @ author Nicholas **/public class FileTest3 {public static void main (String [] args) {try {Reader reader = new FileReader (D: \ javawork \ JavaIOTest \ fileoutputStream.txt); int data = reader. read (); while (data! =-1) {System. out. print (char) data); data = reader. read ();} reader. close ();} catch (IOException e) {e. printStackTrace ();} try {Writer writer = new FileWriter (D: \ javawork \ JavaIOTest \ fileoutputStream.txt); // write the string writer directly. write (Hello This is test); writer. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

 

MPs queue-based thread Communication

Package com. weixuan. test; import java. io. IOException; import java. io. pipedInputStream; import java. io. pipedOutputStream;/*** the Java IO pipeline provides communication capabilities for two threads running in the same JVM. Therefore, pipelines can also be used as data sources and Target media. * Java pipelines are different from those in Unix/Linux systems. In Unix/Linux, * two processes running in different address spaces can communicate through pipelines. In Java, the communication parties should be different threads running in the same process. ** A PipedInputStream stream should be associated with a PipedOutputStream stream. * Data written by one thread through PipedOutputStream can be read by another thread through the Associated PipedInputStream. ** Remember to assign two associated pipeline streams to different threads. * When calling the read () and write () methods, the stream is blocked. This means that if you try to read and write data simultaneously in a thread, the thread may be deadlocked. ** @ author Nicholas **/public class PipedTest {public static void main (String [] args) throws IOException {final PipedOutputStream pipedOutputStream = new PipedOutputStream (); // pipedOutputStream. connect (pipedInputStream); // you can call pipedOutputStream initialization, or call connect to associate final PipedInputStream pipedInputStream = new PipedInputStream (pipedOu TputStream); Thread thread1 = new Thread (new Runnable () {@ Overridepublic void run () {try {pipedOutputStream. write (This is thread1' input !. GetBytes (); pipedOutputStream. close ();} catch (IOException e) {e. printStackTrace () ;}}); Thread thread2 = new Thread (new Runnable () {@ Overridepublic void run () {try {int data = pipedInputStream. read (); while (data! =-1) {System. out. print (char) data); data = pipedInputStream. read ();} pipedInputStream. close ();} catch (IOException e) {e. printStackTrace () ;}}); thread1.start (); thread2.start ();}}

 

Conversion between byte stream and byte stream

Package com. weixuan. test; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStreamReader; import java. io. outputStreamWriter; import java. io. reader; import java. io. writer; import com. sun. jndi. url. corbaname. corbanameURLContextFactory; public class Convert {public static void main (String [] args) {// TODO Auto-generated method stub // Convert byte to character File file = new File (D: \ javawork \ JavaIOTest \ fileoutputStream.txt); // character output stream Writer writer = null; try {writer = new OutputStreamWriter (new FileOutputStream (file); writer. write (Hello); writer. flush (); writer. close ();} catch (IOException e) {e. printStackTrace ();} Reader reader = null; try {reader = new InputStreamReader (new FileInputStream (file); char [] c = new char [1024]; int length = reader. read (c); System. out. println (length); reader. close ();} catch (IOException e) {e. printStackTrace ();}}}


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.