Basics of Java io (top)

Source: Internet
Author: User

Java IO Overview
io is a central issue for the entire human-computer interaction, because IO is the primary channel for computers to acquire and exchange data.
The entire IO process is the source data---> Program---> Destination
Classification of IO
  • Character manipulation-based IO interface: reader,writer
  • BYTE operation-based IO interface: Inputstream,outputstream
  • Disk operation-based IO interface: File
  • Network operation-based IO interface: socket (not under java.io package, but the nature of the socket is IO operation)

A program requires InputStream or reader to read data from a data source, requiring OutputStream or writer to write data to the target medium.

InputStream and reader are associated with the data source, and OutputStream and writer are associated with the target medium.

Understanding of input and output streams

Input stream: Can be understood as input to memory

Output stream: Can be understood as output from memory
Io class overview

BYTE-based IO operation interface


Character-based IO operation interface


Character-to-byte conversion interface data persistence and network transmission are in bytes, so there must be conversions from character to byte or from byte to character.


InputStreamReader is a byte-to-character conversion bridge, and the encoding character set is specified in the process from InputStream to reader. Streamdecoder is exactly the implementation class OutputStreamWriter class that completes decoding from byte to character to complete the encoding process from character to Byte, which is done by Streamencoder.

File Operation Demo

Package Com.weixuan.test;import java.io.file;/** * Learn file-related operations the file class can only complete access to files and file metadata (referring to the file's attribute information) * If you want to read and write the contents of the file, Need to use FileInputStream, FileOutputStream or Randomaccessfile *  * @author Nicholas *  */public class Filetest {public static void Main (string[] args) {//1, must have a file object before using File File = new file ("D:\\centos-7.0-1406-x86_64-dvd.iso") ;//2, detection file existence Boolean isexists = File.exists (); System.out.println (isexists);//3, file length long length = File.length (); System.out.println (length);//4, rename or move file Boolean issuccess = File.renameto (new file ("Test2.iso")); System.out.println (issuccess);//5, delete file Boolean isdelete = File.delete (); System.out.println (isdelete);//6, the detection of a path is a file or directory Boolean isdirectory = File.isdirectory (); System.out.println (isdirectory);//7, read the list of files 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 manipulate files * * @author Nicholas * * FileOutputStream can write a byte stream to a file (that is, from memory output, write to file) * FileInputStream can read the contents of the file in the form of a byte stream (read from the file, write to memory) */ public class FileTest2 {private static outputstream outputstream;private static InputStream inputstream;public static Voi D main (string[] args) {try {//true means append file, false means overwrite outputstream = new FileOutputStream ("d:\\javawork\\javaiotest\\ FileoutputStream.txt ", false);//byte[] C = {' H ', ' e ', ' l ', ' l ', ' o ', ' w ', ' O ', ' r ', ' L ', ' d '};outputstream.write (" Hellow Orld ". GetBytes ());/** * When you write data to FileOutputStream, it is possible that the data will be cached in memory. * At some later time, for example, only x data can be written each time, or the FileOutputStream is closed, it will be actually written to disk. * When FileOutputStream is not turned off and you want to make sure that the data written to FileOutputStream is written to disk, * You can call the flush () method, This method guarantees that all data written to FileOutputStream will be written to disk. */outputstream.flush (); Outputstream.close ();} CAtch (IOException e) {e.printstacktrace ();} Read the file's byte stream try {inputstream = new FileInputStream ("D:\\javawork\\javaiotest\\fileoutputstream.txt"); int data = Inputstream.read (); while (data! = 1) {System.out.println (Inputstream.read ());d ATA = 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\\fileout PutStream.txt "); int data = Reader.read (); while (data! = 1) {System.out.print (char) data);d ATA = Reader.read ();} Reader.close ();} catch (IOException e) {e.printstacktrace ();} try {writer writer = new FileWriter ("D:\\javawork\\javaiotest\\fileoutputstream.txt");//write directly to the string Writer.write ("Hello This is Test "); Writer.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

Pipeline-based thread communication

Package Com.weixuan.test;import Java.io.ioexception;import Java.io.pipedinputstream;import java.io.pipedoutputstream;/** * The pipeline in Java IO provides the ability to communicate with two threads running in the same JVM. So pipelines can also be used as data sources as well as target media. * The Java pipeline differs from the pipeline in the Unix/linux system. In Unix/linux, * Two processes running in different address spaces can communicate through a pipeline. In Java, both sides of the communication 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 that when you use two associated pipe flows, be sure to assign them to different threads. * the Read () method and the Write () method call can cause a stream to block, which means that if you try to read and write at the same time in one thread, it can cause thread deadlock. * * @author Nicholas * */public class Pipedtest {public static void main (string[] args) throws IOException {final Pipedo Utputstream PipedOutputStream = new PipedOutputStream ();//Pipedoutputstream.connect (PipedInputStream);// You can call PipedOutputStream initialization, or you can call connect associated final PipedInputStream PipedInputStream = new PipedInputStream ( PipedOutputStream); Thread thread1 = new Thread (new Runnable () {@Overridepublic void run () {try {pipedoutputstream.write ("This is Thread1 ' INP UT! ". GetBytes ());p ipedoutputstReam.close ();} catch (IOException e) {e.printstacktrace ();}}}); Thread thread2 = new Thread (new Runnable () {@Overridepublic void run () {try {int data = Pipedinputstream.read (); while (dat A! =-1) {System.out.print (char) data);d ATA = Pipedinputstream.read ();} Pipedinputstream.close ();} catch (IOException e) {e.printstacktrace ();}}}); Thread1.start (); Thread2.start ();}}

Conversion of Byte stream and character 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//byte converted 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 ();}}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Basics of Java io (top)

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.