Java IO Summary

Source: Internet
Author: User

This article idea is good, the original link: http://blog.csdn.net/yczz/article/details/38761237

Summarize Java IO and record your learning process

First look at the graph (this is only part of the IO, the View API will find much larger than this)

One Java IO start

IO refers to a stream, which starts with a file, and the related processing class is the type

 PackageCom.shangcg.io;ImportJava.io.File;/** * File class Brief (just a brief description of common, not all) * 1 A constructor for the file class with 4 public * new File (String) * New File (string,string) * NEW File (file,string) * New File (URI) * 2 Creation Method * 1.boolean CreateNewFile () does not exist return true existence return False 2.boolean   mkdir () Create directory 3.boolean mkdirs () Create a multilevel directory 3 Delete method 1.boolean Delete () 2.boolean deleteonexit () file Delete after use is complete        4 Judgment Method 1.boolean CanExecute () determine if the file can be executed 2.boolean canRead () Determine if the file is readable 3.boolean canWrite () Determine if the file is writable        4.boolean exists () determine if the file exists 5.boolean isdirectory () 6.boolean isfile () 7.boolean Ishidden () 8.boolean Isabsolute () determine if an absolute path file does not exist also can be judged 5 get method 1.String GetName () 2.String GetPath () 3.Strin G GetAbsolutePath () 4.String getParent ()//If no parent directory returns null 5.long lastmodified ()//Get Last modified time 6.long len Gth () 7.boolean Renameto (File f) 8.file[] listroots ()//Get Machine drive letter 9.string[] List () 10.string[] L IST (filenamefilter FilteR) *  */ Public classFiledemo { Public Static voidMain (string[] args) {createFile (); }               /*** File Processing example*/         Public Static voidCreateFile () {File F=NewFile ("e:/computer desktop/jar/files/create.txt"); Try{f.createnewfile (); //when and only if there is no file with the name specified by this abstract pathname, a new empty file is not created. SYSTEM.OUT.PRINTLN ("The Partition Size" +f.gettotalspace ()/(1024*1024*1024) + "G");//returns the name of the file or directory represented by this abstract path name. F.mkdirs ();//creates the directory specified by this abstract pathname, including all required but nonexistent parent directories. SYSTEM.OUT.PRINTLN ("filename" +f.getname ());//returns the name of the file or directory represented by this abstract path name. System.out.println ("File parent directory String" +f.getparent ());//returns the pathname string of the parent directory for this abstract pathname, or null if the pathname does not specify a parent directory. }Catch(Exception e) {e.printstacktrace (); }        }  }

Two-byte stream (usually used for image, audio, etc. processing)

From the beginning of the picture

InputStream this abstract class is a superclass of all byte-based input streams, outputstream this abstract class is a superclass of all byte-based output streams

 PackageCom.shangcg.io;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream;/*** InputStream Brief * * 1 read-in Related method: read-In Related method is the core method of this class. There are 3 overloaded forms * 1 int read () reads the next byte of stream, reaches the end of stream return-1 * 2 int read (byte b[]) * 3 int read (byte b[], int off    , int len) * * 2 int available () read (call read) or skip to detect how many bytes are available * 3 long skip (long n) skips n bytes of the current stream, returning the number of bytes actually skipped * 4 void mark (int readlimit) is used to mark the current position of the stream, and the parameter readlimit specifies that the tag's "validity" * 5 void Reset () is used to relocate to the nearest marker. If the Mark method has never been called before, or if the token is invalid, throw IOException * 6 Boolean marksupported () to detect whether the current stream object supports markup. is to return true. Otherwise returns false * 7 void Close () closes the current stream, frees resources associated with the stream, and prevents resource leaks **/ Public classInputstreamdemo { Public Static voidMain (string[] args)throwsioexception{/**Available Demo start*/        /**Summary: This method is usually not used alone and is usually called to detect the number of readable bytes before reading the stream*/InputStream is=NULL; Try{ is=NewFileInputStream ("D:/test.txt"); System.out.println ("Available:" +is.available ()); } finally {              if(Is! =NULL) {is.close (); }          }          /**Available Demo End*/                /**Read () demo start*/        /**Description: Reads the next byte of the input stream, returns an int type integer between 0-255, reaches the end of the stream, returns 1*/        Try{ is=NewFileInputStream ("D:/test.txt"); inti;  while((i = Is.read ())! = 1) {                  //Here I is the ASCII decimal number corresponding to the character, need to convert char, do not believe to remove the testSystem.out.println ("Out:" + (Char) (i); }        } Catch(Exception e) {e.printstacktrace (); } finally {              if(Is! =NULL) {is.close (); }          }           /**Read Demo End*/    }}

Not to be completed tomorrow supplement

Java IO Summary

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.