当前位置:网站首页>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 objectTwo . Character stream
1.
Reader class :FileReader and BufferedReader
Writer class :FileWriter and BufferdeWriter2. 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 gb2312Pattern: The compiled representation of regular expressions
Matcher: The engine that performs the matching operationinputStreamReader: 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 connection5. 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: Syncstart(): 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 classAll with Stream At the end are byte streams
All with Reader/Writer It ends with a stream of charactersInput Output
Byte stream InputStream OutputStream
Character stream Reader Writer
bridge : byte -> character
InputStreamReaderOutputStreamWriter
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();
}
}
}
边栏推荐
- Rustup installation
- Several common ways for Flink to extract eventtime and generate watermark
- MySQL learning summary 8: addition, deletion and modification of data processing
- Three ways of scala string interpolation
- [azure data platform] ETL tool (6) -- re understanding azure data factory
- Masa Auth - SSO and Identity Design
- MySQL group commit
- LVS four - tier Load Balancing Cluster (5) LVS Overview
- Redis memory optimization and distributed locking
- Solution of Kitti data set unable to download
猜你喜欢
Feign based remote service invocation
Panel data set of rural cities and towns: per capita consumption and expenditure of prefecture level cities 2012-2019 & rural data of provinces 2013-2019
Masa auth - SSO and identity design
基于华为云物联网设计的浇花神器(STM32+ESP8266)
简述:分布式CAP理论和BASE理论
MySQL learning summary 12: system variables, user variables, definition conditions and handlers
Data of all bank outlets in 356 cities nationwide (as of February 13, 2022)
Get to know druid IO real time OLAP data analysis storage system
Complex network analysis capability based on graph database
Summary of rust language practice
随机推荐
Spark Foundation
Polymorphism in golang
Several common ways for Flink to extract eventtime and generate watermark
A data modeling optimization solution for graph data super nodes
C语言程序设计——从键盘任意输入一个字符串(可以包含:字母、数字、标点符号,以及空格字符),计算其实际字符个数并打印输出,即不使用字符串处理函数strlen()编程,但能实现strlen()的功能。
Cross border M & a database: SDC cross border database, Thomson database, A-share listed company M & a database and other multi index data (4w+)
Redis memory optimization and distributed locking
[azure data platform] ETL tool (2) -- azure data factory "copy data" tool (cloud copy)
Spark kernel (execution principle) environment preparation /spark job submission process
LVS four layer load balancing cluster (6) LVS working mode
Technical documentbookmark
MySQL learning summary 9: non empty constraints, uniqueness constraints, primary key, auto_ Increment, foreign key, default, etc
Sparksql of spark
Understanding the ongdb open source map data foundation from the development of MariaDB
Two Chinese vector map data with map review number
MASA Auth - SSO與Identity設計
Get to know druid IO real time OLAP data analysis storage system
Summary of virtualization technology development
Several functions in YAF framework controller
Isolation level, unreal read, gap lock, next key lock