Java Learning Note (i): an example of a small reptile

Source: Internet
Author: User
Tags ming

1.import java.io.*;

Java.io.* is not a file, but a set of classes. It is all the classes in the Java.io package, * is a wildcard, for example a*.txt represents all txt files that start with a, "? "is a wildcard of a single word, such as a?. TXT represents a TXT file that starts with a and has a name of only two words.

The import function is: When you write a class. There are other classes in it, and the class you're using is not in the same package as the one you're writing, you need to import the full name of that class.

2. The role of int main () similar to the C language

 Public class Main {      publicstaticvoid main (string[] args) {  

3.String

// define the links  that will be accessed String url = "http://www.baidu.com";   // defines a string used to store Web  page content String result = "";  

See http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html

4.Bufferedreader

// defines a buffered character input stream   null;

The BufferedReader class is used to buffer read characters, encapsulate a byte stream into a BufferedReader object, and then read the stream of characters line by row with ReadLine () until a newline character is encountered (equivalent to repeatedly invoking read () of the Reader class object) method to read multiple characters)

See http://blog.csdn.net/caixiexin/article/details/6719482

BufferedReader Class

The BufferedReader is extended by the reader class, providing a common buffered text read, and providing a useful readline that reads a line of text, reads text from the character input stream, buffers individual characters, and provides characters, Efficient reading of arrays and rows.

General usage:

New  BufferedReader ( new InputStreamReader (new FileInputStream ("Ming.txt"null;    while (data = Br.readline ()) =null) {System.out.println (data); }

5.try{}catch (excpetion e) {}finally{}

Used to catch exceptions

Catch (Exception e) {      System.out.println ("Send GET Request exception!") "+ e);      E.printstacktrace ();  Print out the error message}  
// use finally to close the input  stream        finally {              try  {                  ifnull) {                      in.close ();                  }               Catch (Exception E2) {                  e2.printstacktrace ();              }          }  
In Java. Close  is the meaning of closing. such as Session.close ();  It means to close the session  and free up memory.
There are also commonly used links to the database connection
After use, that is, the end of a function code , at the same time write Connection.close (); free memory

6.new

// to turn a string into a  URL object New

For example, there are now two classes, T1 and T2.

T1 a=new T1 ();

T2 b=new T1 ();

Is the new instance of the two T1; you can think of T1 as a mold, new T1 (); just make a thing out of this mold and new do it a few times, so it's not a thing to do, but it's like.
T2 b=new T1 (); If T1 inherits from T2 then the program is correct, otherwise it is the error code. If it is correct, T2 B is declaring that B is the T2 type to be created, and new T1 (); Is creating a T1 type assignment to B, because T1 is a subclass of T2, so it is allowed, if T1 and T2 have the same name as the return method, then the call is in T1.
7.Connection
// Initialize a connection  to that URL URLConnection connection = Realurl.openconnection ();  
Public URLConnection OpenConnection ()
Throws IOException
Returns a URLConnection object that represents the connection to the remote object referenced by the URL.
Each time the OpenConnection method of the protocol handler that invokes this URL opens a new connection.

If a URL's protocol (for example, HTTP or JAR) exists for a public, private URLConnection subclass that belongs to either of the following packages or one of its child packages: Java.lang, java.io, Java.util, java.net, the returned connection will be the type of the subclass. For example, for HTTP, HttpURLConnection will be returned and Jarurlconnection will be returned for the JAR.
Return:
To the URL of the URLConnection.
// start the actual connection   Connection.connect ();  

8. Initialize BufferedReader

// initializes the BufferedReader input stream to read the response  of the URL New BufferedReader (new InputStreamReader (Connection.getinputstream ()));  

InputStreamReader class

It is a bridge of bytes flowing to the character stream, sealed with InputStream inside, it reads one character at a time in a higher order, in text format input/output, you can specify the encoding format;

General usage:

New    InputStreamReader (new FileInputStream ("Ming.txt"while (ch = isr.read ())!=-1) {    System.out.print ((char) ch); }

Examples such as the following

 Public StaticString gethtmlsource (string url)throwsmalformedurlexception, IOException {urlconnection UC=Newurl (url). OpenConnection (); Uc.setconnecttimeout (10000); Uc.setdooutput (true); InputStream in=NewBufferedinputstream (Uc.getinputstream ()); InputStreamReader Rd=NewInputStreamReader (In, "gb2312");intc = 0; StringBuffer Temp=NewStringBuffer (); while((c = Rd.read ())! = 1) {temp.append (Char) c);} In.close ();returntemp.tostring ();}

9. Crawl the data and store it

// used to temporarily store data  for each row crawled to String line;    while NULL {      // traverse each row that is fetched and store  it in result    result + = line ;  }  

10. Output Information System.out.println

 public  class   Test { public  static  void   Main (string[] args) { int  x = 5;   int  y = 2;   int  a=5,b=4,c=3;  SYSTEM.OUT.PRINTLN (x  +y+ "K" ); SYSTEM.OUT.PRINTLN ( "abc" +a+b+c+3); The result is: 7kabc5433  

First, this has nothing to do with the println output rule.
This is about the arithmetic rules and the "+" connectors.
The next x,y,a,b,c are int, which is the type of the "+" number that can be calculated
At last
First output x+y+ "K"
The x+y is calculated from left to right, the result is 7 is the int type, where the "+" number is the calculation symbol, and the "+" sign is when the string connection symbol is processed so that the last int of 7 and string type "K" connection is generated after the new string, "7K"
The second output of "ABC" +a+b+c+3
It is also calculated from left to right, because "abc" is a string, followed by the "+" sign when the string is concatenated with the symbol, so it is "abc5433"
Followed by the "+" sign as a string connection symbol

In summary, when a string appears before and after the "+" number, it is treated as a string join symbol.
For example:
int a=1;
String s= "a";
System.out.println (A+s);
Output "1a"
System.out.println (S+a);
Output "A1"

And this situation from left to right, and then see the "+" number before and after the string appears on the line.



Java Learning Note (i): an example of a small reptile

Related Article

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.