当前位置:网站首页>Advanced API review

Advanced API review

2022-06-13 03:37:00 Mouth strong programmer

             Always review
One . Operation package of file io
1.File class :input( Input ) output( Output )
(1)isFile: Determine if it's a document
(2)isDirectory: Determine if it's a directory ( Folder )
(3)isHidden Determine whether it is a hidden file
(4)exists Determine whether the associated file exists
(5)createNewFile Create a file
(6)mkdir Create a folder
(7)getPath   Get the path to the file
   getName   Get the filename
   getParent Get the name of the last parent directory of the file ( Parent path )
(8)length Get file length , In bytes
(9)lastModified Get the last modification time of the file The return type is long
(10)delete Delete the file with the specified path
(11)list   Get the names of all the files under the folder
    listFile Get all the file objects under the folder
(12)listRoots Get the drive letter of the current system


2. read (Input)
(1)FileInputStream: File reading  .read
(2)byte[] bs = new byte[(int)file.length()]; Make the length of byte array consistent with the file length
(3)String str = new String(bs) Convert byte array to string
(4).close; Closed flow


3. Write (Output)
(1)FileOutputStream: File write .write
(2)byte[] bs = str.getBytes();   take String Convert to byte array
(3) Solve the coverage problem ( Read out the contents first And then write it together with the user input )
Be careful : The buffer stream must be closed first , Turn off other flows


4. Buffer flow (BufferedInputStream && BufferedOutputStream)
(1)BufferedInputStream The basic usage and FileInputStream It's almost the same
But buffering the stream reduces the number of disks IO The cost of , Its performance is higher than FileInputStream and FileOutputStream
(2) serialize : Convert an object from memory to media ( Media on the hard disk ) The process of
  Each object to be serialized must implement one Serializable Interface
(3) Deserialization : Restore objects from hard disk to memory   Read the contents of the object

Two . Character stream
1.
Reader class :FileReader and BufferedReader 
Writer class :FileWriter and BufferdeWriter

2. The Internet (.net)
(1) Get your own computer :InetAddress.getLocalHost()
(2) Get other people's computers in the LAN :InetAddress.getByName("ip Address ");
(3) Get computers on the Internet :URL url = new URL( For example, Baidu ) InputStream is = url.openStream();


3. Convert byte stream to character stream —— Using bridges :InputStreamReader 
Be careful : In the process of converting a byte stream to a character stream , The code may be garbled , You need to set the encoding method , The only encoding method that supports Chinese is 3 Kind of , Namely :gbk utf-8 gb2312

Pattern: The compiled representation of regular expressions
Matcher: The engine that performs the matching operation

inputStreamReader: The read stream of bytes --> Read stream of characters
outputStreamWriter: Write bytes to the stream --> Character write stream


4.
The general idea of chatting between client and server :
* 1. Start the server
* 1.1 Wait for the client to go online
* 2. The client connects to the server
* 3. The client sends information to the server
* 4. The server receives information from the client
* 5. The server replies to the client information
* 6. The client receives server information
* 7. Close all connections


The general idea of downloading files :
 * 1. Start the server and wait for the client to go online
 * 2. The client connects to the server
 * 3. The client tells the server the file name to download
 * 4. The server receives the file name and finds the corresponding file locally
 * 5. The server reads the file from the hard disk to the memory , And then write it to the network
 * 6. The client downloads the file from the network , And save to local
 * 7. Close the connection

5. The use of multithreading ( Flying Pig 、 Slot machine games )
currentThread: Current thread  
setPriority(Thread.MAX_PRIORITY); Top priority
setPriority(Thread.MIN_PRIORITY); Lowest priority

.yield();// Discard current CPU resources Start the next grab again
.join();// This thread takes precedence Keep other threads waiting
.interrupt();// interrupt sleep Threads
.suspend();// Suspend the thread
.resume();// Resume thread execution
.wait();// wait for : Let resources out
.notify();// Wake up the
.notifyAll();// Wake up all
synchronized: Sync

start(): Possess the ability to rob CPU Qualifications
run(): When you get it, execute run 

// Interview questions :100 Addition and subtraction within Factory production and sales

// Multi person chat room :
    1、 Start the service ( A multithread is specially developed to connect )
        1.1. Get information about the current client
         1.2. Send this message to all my clients
    2、 Between customers read   Write     


6、udp: There are three kinds of :
    1、 unicast : It refers to sending packets to only one fixed machine
    2、 radio broadcast : Is to 255.255.255.255 send out , All machines listening to a port can receive
    3、 Multicast : Is to send to a user group , All users in this group can receive ( Such as 224.0.0.0)

Four ancestors :
abstract class

All with Stream At the end are byte streams
All with Reader/Writer It ends with a stream of characters

     Input         Output

Byte stream  InputStream  OutputStream
Character stream  Reader       Writer


bridge : byte -> character
InputStreamReader

OutputStreamWriter

int i = 1;

run Method is the core method soul

 

package com.zking.test;

import java.io.Serializable;

public class Demo_01  {
	public static void main(String[] args) {
		/**
		 *  senior API Review outline 
		 * 	1.IO flow (io)
		 * 		day_01 File class 
		 * 		1.1 File summary , Method ( Construction methods and common methods )
		 * 		1.2  Common methods 
		 * 			 Judgment function :isFile,isDeritory,exists,isHidden
		 * 			 New features :createNewFile,mkdir,mkdirs
		 * 			 Delete function :delete
		 * 			 Acquisition function :getPath,getName,length,lastModified...
		 * 			list,listFiles
		 * 		1.3  Algorithm ( A recursive algorithm )
		 * 		1.4  Find all the file information in a directory 
		 * 
		 * 		day_02 IO Byte stream of stream 
		 * 			1.1  Byte stream 
		 * 				 Input stream --InputStream--FileInputStream--BufferedInputStream
		 * 				 Output stream --OutputStream--FileOutputStream--BufferedOutputStream
		 * 			1.2  Method 
		 * 				read()|write()|flush()
		 * 
		 * 		day_03
		 * 			1.1  Serialization and deserialization ---- flow 
		 * 				 serialize : Save the objects generated in memory to the local file through serialization ‘
		 * 				 Deserialization : Read the object in the text into memory 
		 * 				 To achieve this process : The serialized object must implement Serializable Interface , Otherwise, the report will be wrong 
		 * 				 serialize : Output stream ---ObjectOutputStream
		 * 				 Deserialization : Input stream --ObjectInputStream
		 * 
		 * 			1.2  Common methods 
		 * 				readObject|writeObject
		 * 
		 * 		day_04
		 * 			1.1  Character stream 
		 * 				 Input ( Read )--Reader--FileReader--BufferedReader
		 * 				 Output ( write in )--Writer--FileWriter--BufferedWriter
		 * 				 Common methods :read|write|newLine|readLine|flush
		 * 			
		 * 			1.2  bridge -- Intermediate conversion flow 
		 * 				 Input --InputStreamReader
		 * 				 Output --OutputStreamWriter
		 * 				 above 2 A stream may consider the problem of coding 
		 * 				 Support Chinese coding :GB2312,UTF-8,GBK
		 * 				 pure English :ISO-8859-1
		 * 
		 * 
		 * 	2.NET Network programming (net)
		 * 		1. Network programming : Is the use NET Package to implement data transmission between multiple devices 
		 * 		2. Realize the three elements of network programming :
		 * 			IP Address | Port number | Network communication transmission protocol    (ISO A network model    TCP|IP agreement )
		 * 
		 * 		3.TCP|IP agreement 
		 * 				TCP agreement : Make a phone call 
		 * 				UDP agreement : email 
		 * 		
		 * 		4.URL Programming 
		 * 			 Can pass URL Objects locate resources on the network , Then download it in a specific way .
		 * 				URL---- Uniform resource locator 
		 * 			(1) adopt URL Location resources 
		 * 			(2) adopt url Object call openStream Method to open or activate network flow 
		 * 			(3) Read and write operation 
		 * 
		 * 		day_05
		 * 			1.Pattern,Matchers    find  group
		 * 
		 * 			2. utilize Socket Socket completion simple conversation 
		 * 				ServerSocket
		 * 				Socket
		 * 
		 * 		day_06 
		 * 			 File download 
		 * 			swing Form version of chat 
		 * 
		 * 	3. Multithreading (lang)
		 * 		1.2 Type of implementation :Thread   Runnable
		 * 
		 * 		2. When to use Thread   When to use Runnable
		 * 			 Multiple threads implement the same thing    Use   Runnable
		 * 			 Multiple threads do different things    Use Thread
		 * 
		 * 		3. The life cycle of a thread 
		 * 			5 Stages ( newly build , be ready , function , Blocking , Death )
		 * 
		 * 		4. Common methods 
		 * 			sleep,join,yield,wait,notify  notifyAll
		 * 
		 * 		
		 * UDP---
		 * 	DatagramSocket
		 * 	DatagramPacket
		 * 	
		 * 
		 * 	Map
		 * 
		 * 
		 * 
		 * 		entrySet
		 */
		
	}
}

Using regular expressions to download multiple images

package com.zking.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo_06 {
	public static void main(String[] args) throws Exception {
//		file:///D:/xb/default.html
		
		
		//1. Read the source code of the web page to the memory for printing 
		URL url = new URL("file:///D:/xb/default.html");
		InputStream is = url.openStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		StringBuffer sb = new StringBuffer();
		String str = "";
		while(null!=(str = br.readLine())) {
//			System.out.println(str);
			sb.append(str+"\n");
		}
//		System.out.println(sb);
//		<img src="images/logo.gif" />
//		<img src="images/a1.gif" width="200" height="217" />
		String regex = "<img\\ssrc=\"([^\\\">]+)\"(\\swidth=\"\\w+\")?(\\sheight=\"\\w+\")?\\s/>";
		Pattern compile = Pattern.compile(regex);
		Matcher matcher = compile.matcher(sb);
		while(matcher.find()) {
//			System.out.println(matcher.group(1));
			//images/logo.gif
			//file:///D:/xb/images/banner.jpg
			downloadImg("file:///D:/xb/"+matcher.group(1));
		}
		
	}
	
	public static void downloadImg(String path) throws Exception {
		System.out.println(path);
		URL url = new URL(path);
		// Intercept the name in the network path of the picture 
		int lastIndexOf = path.lastIndexOf("/");
		String fileName = "";
		if(-1!=lastIndexOf) {
			fileName = path.substring(lastIndexOf+1);
		}
		
		InputStream is = url.openStream();
		BufferedInputStream bis = new BufferedInputStream(is);
		
		
		File file = new File("D:\\"+fileName);
		FileOutputStream fos = new FileOutputStream(file);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		byte[] buf = new byte[1024];
		int len = 0;
		while(-1!=(len = bis.read(buf))) {
			bos.write(buf,0,len);
			bos.flush();
		}
		
		
	}
	
	
}

原网站

版权声明
本文为[Mouth strong programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280529564002.html