IO Read-write I--"reader
O--> Writer
Create a character stream to facilitate the manipulation of characters or text files
Especially the buffered character input and output stream (bufferedreader,bufferedwriter) ReadLine.
And the bottom is still using the word stream
In particular, the buffer byte input/output stream (bufferedinputstream,bufferedoutput) Read (character array, Offset,len) method is so Good.
/**
* I/O Flow: I->input o->output
* I/O flow: for data transfer between different devices
* I/O transfer in Java writing code
* I/O is embodied in Java: there is a system: encapsulated in the IO package
* Data transmission is divided into:
* Byte stream (data is transmitted in bytes):
*
* Character Stream (data is transmitted in bytes):
* All operations files, text based on characters, creating a character stream
*
* AscII (americans) A-65 a-97
*
* Kanji: GB2312 Encoding table (thousands of characters Stored)
* Chinese characters: GBK encoding table (tens of thousands of characters are stored)
* Chinese characters: 18030 encoding table (56 family Members)
*
* International Code table: utf-8 (unicode)
*
* Character stream is based on byte stream: only so to use character stream: convenient
*
* IO package: There are four base classes
* Byte stream base class: Inputstream,outputstream
* Character Stream base class: Reader,writer
*
* There will be a lot of sub-categories: FileInputStream
* (with the current base class name suffix, the preceding name code is the specific Operand)
* FileReader
* (with the current base class name suffix, the preceding name code is the specific Operand)
*/
/**
* IO stream: new FileWriter (fileName); This construction method:
* If the file exists in the current directory, create a new file of that name,
* and overwrite the previous File. no matter what, a new file will be created
* When calling this constructor method: if the specified path does not exist, it will produce a filenotfoundexeception
*/
/* grasp four base classes, InputStreamReader = InputStream + reader that is, the object is InputStream and the objects after operation are reader
Easy-to-understand way: byte stream is low-level stream, character Stream advanced stream, the Advanced Stream is based on low-level stream, in order to read more convenient, character to Byte is the computer only know low-level stream
Because the bottom of the computer only recognizes the binary 0 1
OutputStreamWriter and InputStreamReader
In the whole IO package, it is actually divided into byte stream and character stream, but in addition to these two streams, there is a set of byte Stream-character stream conversion class.
Outputstreamwriter: is a writer's subclass that converts the output character to a byte stream, that is, the output object of a character stream becomes the output object of the byte Stream.
Inputstreamreader: is a subclass of reader that converts the input byte stream into a character stream, that is, the input object of a stream of bytes into the input object of a character Stream.
It is generally necessary to use a byte or character stream in the operation of the input and output, but sometimes it is necessary to convert the character stream into the form of a byte stream, or a stream of bytes into the form of a character stream, so that another set of operation classes of the flow is Needed.
Description of FileWriter and FileReader
You can tell from the JDK document that FileOutputStream is a direct subclass of Outputstream. FileInputStream is also a direct subclass of inputstream, But there are some special types of two operations in a character stream file,
FileWriter is not directly a writer's subclass, but a subclass of outputstreamwriter, and FileReader is not directly a subclass of reader, but a subclass of inputstreamreader,
Then the inheritance of these two classes can be clearly found, whether the use of a byte stream or a character stream is actually ultimately in the form of bytes operation of the input and output Stream.
In other words, when transmitting or reading data from a file, the data that is actually stored in the file is always Bytes.
*/
/* read three forms of keyboard input */import java.io.*;import java.util.*;import java.util.scanner;class mytest{public static void Main ( String [] Args) throws Exception{//method one scanner scanner =new scanner (system.in); String Inputstr=scanner.nextline (); System.out.println (inputstr);//method two BufferedReader bufreader=new bufferedreader (new inputstreamreader (system.in)); String line=null;while ((line=bufreader.readline ())!=null) {System.out.println (line);}}} /* simplify */class abrreviatedemo{public static void main (string [] Args) {string str= "II ... Lll...ove. ee.....you! "; String regex= "\\.+"; String replacestr= ""; str=retrivestr (str,regex,replacestr); regex= "(.) \\1+ ", replacestr=" $ "; str=retrivestr (str,regex,replacestr); System.out.println (str);} public static string Retrivestr (string str,string regex,string replacestr) {return str.replaceall (regex,replacestr);}} /* sort a bunch of cluttered IP addresses */import java.util.*;class sortip{public static void main (string [] Args) {string ip= "192.168.0.5 2.2.3 .4 127.0.0.1 ";p Rintaftersort (IP);} public staticvoid Printaftersort (string str) {string regex= "(0*\\d+)"; str=str.replaceall (regex, "00$1"); regex= "0* (\\d{3})"; str= Str.replaceall (regex, "$"); System.out.println (str); regex= "+"; String [] Strarray=str.split (regex); Arrays.sort (strarray); for (int i=0;i<strarray.length;i++) {System.out.println (strarray[i].replaceall (0*) "," $ ")); System.out.println (str); }}/* E-mail address check */class checkmaildemo{public static void main (string [] Args) {string str= "[email protected]"; System.out.println ("result:" +checkmail (str));//string regex= "";} public static Boolean Checkmail (string str) {string regex= "[a-za-z0-9_]+[@][a-za-z0-9]+ (\\.[ A-za-z]+) {1,3} "; regex=" \\[email protected]\\w+ (\\.\\w+) {1,3} "; return str.matches (regex);}} /* get the required phone number from a bunch of messy strings */import java.util.regex.*;class regexdemo{public static void main (string [] Args) {string str= " 1AFASDF13874057617WEOJFJLJ "; String regex= "[1-9][3,5,8]\\d{9}"; retrivestr (str,regex);} public static void Retrivestr (String str,string Regex) {Pattern P=pattern.compiLe (regex); Matcher m=p.matcher (str); while (m.find ()) {String tempstr=m.group (); System.out.println (tempstr);}}} /* read keyboard standard input stream and print uppercase to console */import java.io.*;import java.util.*;class upercasesystemin{public static void main (String [] Args) throws Ioexception{inputstream In=system.in;doupcasereadin (in);} public static void Doupcasereadin (inputstream In) throws Ioexception{bufferedreader bufr=new BufferedReader (new InputStreamReader (in)); String str=null;while ((str=bufr.readline ())!=null) {System.out.println (str.touppercase ()); Str.equalsignorecase ("exit")) system.exit (0); break}}}/* read a bar e-mail address and print it to the console note here the regular */import java.net.*;import java.io.*;import java.util.*;import Java.util.regex . *;class spidertest{public static void main (String [] Args) throws Exception{url url=new URL ("http://tieba.baidu.com/p/ 2314539885 "); Getemailaddressfromurl (url);} public static void Getemailaddressfromurl (url Url) throws Exception{urlconnection urlconn=url.openconnection (); BufferedReader Bufreader = new BufferedreAder (new InputStreamReader (urlconn.getinputstream ())); String line=null; String regex= "\\[email protected]\\w+ (\\.\\w+) +"; Pattern p=pattern.compile (regex), while ((line=bufreader.readline ())!=null) {Matcher m = p.matcher (line), while (m.find ()) {System.out.println (m.group ());} System.out.println (line);}}} /* implement local binary file copy */import java.io.*;class copyimg{public static void main (String [] Args) {bufferedinputstream bufinps=null ; Bufferedoutputstream bufotps=null;try{bufinps=new bufferedinputstream (new fileinputstream ("psb.jpg")); bufotps=new Bufferedoutputstream (new fileoutputstream ("psb_copy.jpg")); byte [] buf=new byte[8192];int len=0;while ((len= Bufinps.read (buf)) >0) {bufotps.write (buf,0,len);//bufotps.flush ();}} Catch (ioexception ioe) {ioe.printstacktrace ();} Finally{if (bufinps!=null) Try{bufinps.close ();} Catch (ioexception ioe) {ioe.printstacktrace ();} If (bufotps!=null) try{bufotps.close ();} Catch (ioexception ioe) {ioe.printstacktrace ();}}}} /* crawl the compliant email address from a webpage and save it to the local disk path */import Java.io.*;import jAva.util.regex.*;import java.net.*;import java.util.*;class spider{public static void main (String [] Args) throws Exception{url url=new URL ("http://tieba.baidu.com/p/2314539885");//string [] emailaddress=arraylist<string> Emaillist=getemailbyurl (url); for (String emailaddress:emaillist) {System.out.println (emailaddress);} String qualifiedname= "c://users//ghc//desktop//test//emailaddress.txt"; writeemailtofile (qualifiedName,emailList );} Read html from urlpublic static arraylist<string> Getemailbyurl (url Url) throws Exception{urlconnection urlconn= Url.openconnection (); BufferedReader bufreader =new bufferedreader (new inputstreamreader (urlconn.getinputstream ()));//regex match Patternstring regex= "\\[email protected]\\w+ (\\.\\w+) +"; Pattern P=pattern.compile (regex); arraylist<string> emaillist=new arraylist<string> (); String line=null;while ((line=bufreader.readline ())!=null) {Matcher m=p.matcher (line);//Start to iterator the one Matches while (m.find ()) {emaillist.add (M.group ());//system.out.println (m.group ());} System.out.println (line);} Return emaillist;} public static void Writeemailtofile (String qualifiedname,arraylist<string> Emaillist) throws exception{ BufferedWriter bufwriter=new bufferedwriter (new FileWriter (qualifiedname)); for (int i=0;i<emaillist.size (); I++) { Bufwriter.write (emaillist.get (i)); bufwriter.newline (); bufwriter.flush ();}} /* Crawl The URL address of a picture from a webpage and download it to the local disk path the basic functionality has been implemented, but the regular need to adjust */import java.net.*;import java.io.*;import java.util.regex.*; Import Java.util.*;class imgspider{public static void main (String [] Args) {saveimgfromurl ("http://image.baidu.com/", " c:/users/ghc/desktop/test/"); System.GC ();} public static Boolean downloadimg (String line,string path) {boolean flag=true; FileOutputStream fos=null; Bufferedinputstream bufinpts=null; Bufferedoutputstream bufopts=null;path=path.replace ("<", "" ");/* System.out.println (line); System.out.println (path); */try{bufinpts=new bufferedinputstream (new URL (line)). openconnection (). getinputsTream ()); fos=new fileoutputstream (path); bufopts=new bufferedoutputstream (fos); byte [] buf=new byte[1024];int len=-1; While ((len=bufinpts.read (buf))!=-1) {bufopts.write (buf,0,buf.length);}} Catch (ioexception ioe) {ioe.printstacktrace (); flag=false;} Finally{if (bufopts!=null) Try{bufopts=null;bufopts.close ();} Catch (ioexception ioe) {ioe.printstacktrace ();} If (fos!=null) try{fos=null; fos.close ();} catch (ioexception ioe) {ioe.printstacktrace ();}} Return flag;} public static Boolean Saveimgfromurl (String urlstr,string folder) {boolean flag=true; URL Url=null;//inputstream in=null; String line=null; BufferedReader bufr=null; Pattern p=null; Matcher m=null; arraylist<string> imglist=null;try{url=new URL (urlstr); URLConnection urlconn=url.openconnection (); bufr=new bufferedreader (new inputstreamreader (urlconn.getInputStream () )); imglist=new arraylist<string> (); String regex= "]*?> ";p =pattern.compile (regex), while ((line=bufr.readline ())!=null) {m=p.matcher (line), while (m.find ()){System.out.println (m.group ()); imglist.add (m.group ());} System.out.println (line);} Iterator<string> it=imglist.iterator (); while (it.hasnext ()) {line=it.next (); folder+=line.substring ( Line.lastindexof ("/", 2) + 1, 3) + ". png"; Http://www.jb51.net/images/logo.gifm=Pattern.compile ("http://(\\w+\\.) +[a-z]+/images/(\\w+\\.) +[a-z]{3} "). matcher (line), while (m.find ()) {line=m.group (),//system.out.println (line);d ownloadimg (line,folder);}} Catch (malformedurlexception mfe) {mfe.printstacktrace (); flag=false;} Catch (ioexception ioe) {ioe.printstacktrace (); flag=false;} Finally{if (bufr!=null) Try{bufr=null;bufr.close ();} Catch (ioexception ie) {ie.printstacktrace ();}} Return flag;}} /* Regular small exercise */class demo{public static void main (string [] Args) {string qq= "1212345"; boolean checkresult=checkqq (qq); System.out.println (checkresult qq+ "is right": qq+ "is wrong!!!"); String telnumber= "15974097817"; Checkresult=checktel (telnumber); System.out.println (checkresult telnumber+ "is right": Telnumber+ "is wrong!!!"); String path= "c:\\users\\frank\\abqqcdkkkefghhijkkkkkl.txt"; String regex= "(.) \\1+ "; Overlapping word cutting note the concept of introduction group, \ n refers to the group of 1 or more QQ or KKK will be treated as a cutting character printaftersplit (path,regex); System.out.println ("================="); regex= "\ \"; Printaftersplit (path,regex); String str= "abcddeffffg"; regex= "(.) \\1{3,} "; String replacestr= "$";p rintafterreplacestr (str,regex,replacestr);} The following two functions of the regular match check string are public static Boolean Checktel (string telnumber) {string regex= "[1][3,5,8]\\d{9}"; return Telnumber.matches (regex);} public static Boolean checkqq (String Qq) {//boolean result=false; String regex= "[1-9][0-9]{4,14}"; regex= "[1-9]\\d{4,14}"; return qq.matches (regex);/* int len=qq.length (); if (len<5 | | len>15 | | qq.startswith ("0") {System.out.println ("length or Startwith issue!!!"); /return result;} else {char [] Ary=qq.tochararray (); for (int i=0;i<ary.length;i++) {if (!) ( ary[i]>= ' 0 ' && ary[i]<= ' 9 ') {System.out.println ("not between 0 and 9!!!"); Break;//return result;} else ResUlt=true;}} *///return result;} The following two functions of the regular cut string are public static void Printaftersplit (string path,string regex) {string [] ary=path.split (regex); String S:ary) {System.out.println (s);}} The following two are all custom regular substitution string functions public static void Printafterreplacestr (string str,string regex,string replacestr) {string Resultstr=str.replaceall (regex,replacestr); System.out.println (resultstr);}}
JAVA IO Packet Summary