python显示rn还是n
java随机数正则九种IO
package main.java.com.cj;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main {
public static void inAndOut(String x, String y, String z) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
com.cj.Test test = new com.cj.Test();
Class> tests = test.getClass();
Method func = tests.getDeclaredMethod(z,String.class,String.class);
func.invoke(test,x,y);
}
public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
String x = "C:\Users\c50019678\Desktop\JAVA\helloworld\src\main\java\com\cj\0.txt";
String y1 = "C:\Users\c50019678\Desktop\JAVA\helloworld\src\main\java\com\cj\1.txt";
String y2 = "C:\Users\c50019678\Desktop\JAVA\helloworld\src\main\java\com\cj\2.txt";
String y3 = "C:\Users\c50019678\Desktop\JAVA\helloworld\src\main\java\com\cj\3.txt";
String y4 = "C:\Users\c50019678\Desktop\JAVA\helloworld\src\main\java\com\cj\4.txt";
String y5 = "C:\Users\c50019678\Desktop\JAVA\helloworld\src\main\java\com\cj\5.txt";
String y6 = "C:\Users\c50019678\Desktop\JAVA\helloworld\src\main\java\com\cj\6.txt";
String y7 = "C:\Users\c50019678\Desktop\JAVA\helloworld\src\main\java\com\cj\7.txt";
String y8 = "C:\Users\c50019678\Desktop\JAVA\helloworld\src\main\java\com\cj\8.txt";
String y9 = "C:\Users\c50019678\Desktop\JAVA\helloworld\src\main\java\com\cj\9.txt";
String a = "copy_by_Byte";
String b = "copy_by_Buffer";
String c = "copy_by_StreamByte";
String d = "copy_by_StreamBuffer";
String e = "reader_by_char";
String f = "reader_by_buffer";
String g = "bf_by_ch";
String h = "bf_by_bf";
String i = "bf_by_line";
inAndOut(x,y1,a);
inAndOut(x,y2,b);
inAndOut(x,y3,c);
inAndOut(x,y4,d);
inAndOut(x,y5,e);
inAndOut(x,y6,f);
inAndOut(x,y7,g);
inAndOut(x,y8,h);
inAndOut(x,y9,i);
}
}
package com.cj;
import java.io.*;
public class Test {
public void copy_by_Byte(String x, String y) throws IOException {
long start =System.currentTimeMillis();
FileInputStream source = new FileInputStream(new File(x));
FileOutputStream to = new FileOutputStream(new File(y));
int i=0;
while((i=source.read())!=-1){
to.write(i);
}
source.close();
to.close();
long end =System.currentTimeMillis();
System.out.println("use "+(end-start)+" ms");
}
public void copy_by_Buffer(String x, String y) throws IOException{
long start =System.currentTimeMillis();
FileInputStream source = new FileInputStream(new File(x));
FileOutputStream to = new FileOutputStream(new File(y));
byte[] b=new byte[16];
int i=0;
while((i=source.read(b))!=-1){
to.write(b);
}
source.close();
to.close();
long end =System.currentTimeMillis();
System.out.println("use "+(end-start)+" ms");source.close();
// String c = Arrays.toString(b); // 这里可以研究一下,出现了/r/n
}
public void copy_by_StreamByte(String x, String y) throws IOException {
long starts = System.currentTimeMillis();
FileInputStream source_tmp = new FileInputStream(new File(x));
BufferedInputStream source = new BufferedInputStream(source_tmp);
FileOutputStream to_tmp = new FileOutputStream(new File(y));
BufferedOutputStream to = new BufferedOutputStream(to_tmp);
int i;
while((i=source.read())!=-1){
to.write(i);
}
source.close();
source_tmp.close();
to.close();
to_tmp.close();
long end =System.currentTimeMillis();
System.out.println("use "+(end-starts)+" ms");
}
public void copy_by_StreamBuffer(String x, String y) throws IOException {
long start =System.currentTimeMillis();
FileInputStream source_tmp = new FileInputStream(new File(x));
BufferedInputStream source = new BufferedInputStream(source_tmp);
FileOutputStream to_tmp = new FileOutputStream(new File(y));
BufferedOutputStream to = new BufferedOutputStream(to_tmp);
byte[] b=new byte[16];
int i=0;
while((i=source.read(b))!=-1){
to.write(b);
}
source.close();
source_tmp.close();
to.close();
to_tmp.close();
long end =System.currentTimeMillis();
System.out.println("use "+(end-start)+" ms");source.close();
}
public void reader_by_char(String x, String y) throws IOException {
long start =System.currentTimeMillis();
FileInputStream source_tmp = new FileInputStream(new File(x));
InputStreamReader source = new InputStreamReader(source_tmp);
FileOutputStream to_tmp = new FileOutputStream(new File(y));
OutputStreamWriter to = new OutputStreamWriter(to_tmp);
int len=0;
while((len=source.read())!=-1){
to.write(len);
}
source.close();
source_tmp.close();
to.close();
to_tmp.close();
long end =System.currentTimeMillis();
System.out.println("use "+(end-start)+" ms");source.close();
}
public void reader_by_buffer(String x, String y) throws IOException {
long start =System.currentTimeMillis();
FileInputStream source_tmp = new FileInputStream(new File(x));
InputStreamReader source = new InputStreamReader(source_tmp);
FileOutputStream to_tmp = new FileOutputStream(new File(y));
OutputStreamWriter to = new OutputStreamWriter(to_tmp);
char[] ch=new char[16];
int len=0;
while((len=source.read(ch))!=-1){
to.write(ch);
}
source.close();
source_tmp.close();
to.close();
to_tmp.close();
long end =System.currentTimeMillis();
System.out.println("use "+(end-start)+" ms");source.close();
}
public void bf_by_ch(String x, String y) throws IOException {
long start =System.currentTimeMillis();
FileReader source_tmp = new FileReader(new File(x));
BufferedReader source = new BufferedReader(source_tmp);
FileWriter to_tmp = new FileWriter(new File(y));
BufferedWriter to = new BufferedWriter(to_tmp);
int i=0;
while((i=source.read())!=-1){
to.write(i);
}
source.close();
source_tmp.close();
to.close();
to_tmp.close();
long end =System.currentTimeMillis();
System.out.println("use "+(end-start)+" ms");source.close();
}
public void bf_by_bf(String x, String y) throws IOException {
long start =System.currentTimeMillis();
FileReader source_tmp = new FileReader(new File(x));
BufferedReader source = new BufferedReader(source_tmp);
FileWriter to_tmp = new FileWriter(new File(y));
BufferedWriter to = new BufferedWriter(to_tmp);
char[] ch=new char[16];
int len=0;
while((len=source.read(ch))!=-1){
to.write(ch);
}
source.close();
source_tmp.close();
to.close();
to_tmp.close();
long end =System.currentTimeMillis();
System.out.println("use "+(end-start)+" ms");source.close();
}
public void bf_by_line(String x, String y) throws IOException {
long start =System.currentTimeMillis();
FileReader source_tmp = new FileReader(new File(x));
BufferedReader source = new BufferedReader(source_tmp);
FileWriter to_tmp = new FileWriter(new File(y));
BufferedWriter to = new BufferedWriter(to_tmp);
String s;
while((s=source.readLine())!=null){
to.write(s);
}
source.close();
source_tmp.close();
to.close();
to_tmp.close();
long end =System.currentTimeMillis();
System.out.println("use "+(end-start)+" ms");source.close();
}
}
package com.cj;
import java.util.regex.Pattern;
public class RE {
public static void main(String[] args){
Pattern ptn = Pattern.compile("^(One|Two|Three).*");
System.out.println(ptn.matcher("On").matches());
System.out.println(ptn.matcher("One").matches());
System.out.println(ptn.matcher("Ones").matches());
System.out.println(ptn.matcher("Tw").matches());
System.out.println(ptn.matcher("Two").matches());
System.out.println(ptn.matcher("Twos").matches());
System.out.println(ptn.matcher("Thr").matches());
System.out.println(ptn.matcher("Three").matches());
System.out.println(ptn.matcher("Threes").matches());
}
}
package com.cj;
import java.security.SecureRandom;
import java.util.Arrays;
public class Rand {
public static void main(String[] args) {
System.out.println("hello world");
System.out.println(Arrays.toString(genRandBytes(5)));
}
public static byte[] genRandBytes(int len) {
byte[] bytes = null;
if (len > 0 && len < 1024) {
bytes = new byte[len];
// 使用SecureRandom类生成随机数
SecureRandom random = new SecureRandom();
random.nextBytes(bytes);
}
return bytes;
}
}
正则的贪婪与懒惰
对进行正则匹配
非懒惰模式正则
src=".*"
结果:src="test.jpg" width="60px" height="80px"
意思是从="往后匹配,直到最后一个"匹配结束
懒惰模式正则:
src="https://www.mshxw.com/skin/sinaskin/image/nopic.gif"
结果:src="test.jpg"
意思是匹配到第一个"就结束了一次匹配,不会继续向后匹配
.表示除n之外的任意字符
*表示匹配0-无穷
+表示匹配1-无穷
java的ssh写法
package com.huawei.udrstartercore.system.controller;
import com.huawei.udrstartercore.util.SSHLinux;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ch.ethz.ssh2.Connection;
@RestController
public class SSHLinuxController {
private static final String IP = "10.90.121.228";
private static final String NAME = "root";
private static final String PASSWORD = "cnp200@HW";
private static final String CMD_SSHD_PID =
"ps -ef | awk '/sshd/{print $2}' | awk 'NR>1{print line}{line=$0}'";
private static final String CMD_SSHD_FIRST_PID =
"ps -ef | awk '/sshd/{print $2}' | awk 'NR==1'";
private static final String CMD =
"sar 1 1";
@RequestMapping(path = {"linuxcpu"})
public String cpu() {
SSHLinux sshLinux = new SSHLinux();
Connection connection = sshLinux.login(IP, NAME, PASSWORD);
System.out.println(connection.toString());
//获取sshd进程的pid命令
// String pids = sshLinux.execute(connection, CMD_SSHD_PID);
// String pid = pids.split(" ")[0];
// System.out.println(pid);
return sshLinux.execute(connection, CMD);
// return connection.toString();
}
}
package com.huawei.udrstartercore.util;
import java.io.UnsupportedEncodingException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class SSHLinux {
private static final Logger log = LoggerFactory.getLogger(SSHLinux.class);
private static String DEFAULTCHART="UTF-8";
public static Connection login(String ip,
String userName,
String userPwd){
boolean flg=false;
Connection conn = null;
try {
conn = new Connection(ip);
conn.connect();//连接
flg=conn.authenticateWithPassword(userName, userPwd);//认证
if(flg){
log.info("=========登录成功========="+conn);
return conn;
}
} catch (IOException e) {
log.error("=========登录失败========="+e.getMessage());
e.printStackTrace();
}
return conn;
}
public static String execute(Connection conn,String cmd){
String result="";
try {
if(conn !=null){
Session session= conn.openSession();//打开一个会话
session.execCommand(cmd);//执行命令
result=processStdout(session.getStdout(),DEFAULTCHART);
//如果为得到标准输出为空,说明脚本执行出错了
if(StringUtils.isBlank(result)){
log.info("得到标准输出为空,链接conn:"+conn+",执行的命令:"+cmd);
result=processStdout(session.getStderr(),DEFAULTCHART);
}else{
log.info("执行命令成功,链接conn:"+conn+",执行的命令:"+cmd);
}
conn.close();
session.close();
}
} catch (IOException e) {
log.info("执行命令失败,链接conn:"+conn+",执行的命令:"+cmd+" "+e.getMessage());
e.printStackTrace();
}
return result;
}
private static String processStdout(InputStream in, String charset){
InputStream stdout = new StreamGobbler(in);
StringBuffer buffer = new StringBuffer();;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(stdout,charset));
String line=null;
while((line=br.readLine()) != null){
buffer.append(line+System.lineSeparator());
}
} catch (UnsupportedEncodingException e) {
log.error("解析脚本出错:"+e.getMessage());
e.printStackTrace();
} catch (IOException e) {
log.error("解析脚本出错:"+e.getMessage());
e.printStackTrace();
}
return buffer.toString();
}
}
Astar
import com.cj.AStar;
import com.cj.Node;
import java.util.List;
public class AstarTest {
public static void main(String[] args) {
AStar aStar = new AStar(5, 5, new Node(0,0), new Node(4,4));
aStar.setBlock(1,3);
aStar.setBlock(2,3);
aStar.setBlock(3,3);
aStar.setBlock(4,3);
List nodeList = aStar.findPath();
System.out.println(nodeList.toString());
}
}
package com.cj;
import java.io.Serializable;
import java.util.*;
public class AStar {
private static final int DEFAULT_HV_COST = 10; // Horizontal - Vertical Cost
private static final int DEFAULT_DIAGONAL_COST = 14; // Diagonal Cost
private final int hvCost; // 横耗
private final int diagonalCost; // 斜耗
private final Node[][] searchArea; // 地图
private final PriorityQueue openList; // 开启列表,优先队列,相当于广搜的下一步搜索结点
private final Set closedSet; // 关闭点集,集合,不再搜索的节点,相当于广搜的已搜节点打标记
private Node initialNode; // 起点
private Node finalNode; // 终点
public AStar(int rows, int cols, Node initialNode, Node finalNode, int hvCost, int diagonalCost) {
this.hvCost = hvCost;
this.diagonalCost = diagonalCost;
setInitialNode(initialNode);
setFinalNode(finalNode);
this.searchArea = new Node[rows][cols];
this.openList = new PriorityQueue<>(new NodeComparator());
setNodes();
this.closedSet = new HashSet<>();
}
public AStar(int rows, int cols, Node initialNode, Node finalNode) {
this(rows, cols, initialNode, finalNode, DEFAULT_HV_COST, DEFAULT_DIAGONAL_COST);
}
private void setNodes() {
for (int i = 0; i < searchArea.length; i++) {
for (int j = 0; j < searchArea[0].length; j++) {
Node node = new Node(i, j);
node.calculateHeuristic(getFinalNode());
this.searchArea[i][j] = node;
}
}
}
public List findPath() {
openList.add(initialNode); // 开启列表加入起点
while (!isEmpty(openList)) { // 当列表非空就循环,空其实就是起点到不了终点
Node currentNode = openList.poll(); // 取出队头的节点
closedSet.add(currentNode); // 关闭集合加入当前节点
assert currentNode != null; // 断言,当前结点非空
if (isFinalNode(currentNode)) { // 当前结点是终点
return getPath(currentNode); // 就传入当前结点返回路径
} else { // 否则
addAdjacentNodes(currentNode); // 把相邻的八个点加入开启列表
}
}
return new ArrayList<>(); // 返回空列表
}
private List getPath(Node currentNode) { // 获取起点到当前点的路径
List path = new ArrayList<>(); // 先初始化一个Node列表作为返回的路径
path.add(currentNode); // 加入当前结点
Node parent; // 声明父结点
while ((parent = currentNode.getParent()) != null) { // 获取父亲非空(空就是到起点)
path.add(0, parent); // 在path列表的表头加入父亲结点(所以最后不用倒序)
currentNode = parent; // 把当前结点标记为父结点
}
return path; // 返回列表
}
private void addAdjacentNodes(Node currentNode) {
addAdjacentUpperRow(currentNode);
addAdjacentMiddleRow(currentNode);
addAdjacentLowerRow(currentNode);
}
private void addAdjacentLowerRow(Node currentNode) {
int row = currentNode.getRow();
int col = currentNode.getCol();
int lowerRow = row + 1;
if (lowerRow < getSearchArea().length) {
if (col + 1 < getSearchArea()[0].length) { // Y未出右界
checkNode(currentNode, col + 1, lowerRow, getDiagonalCost()); // Comment this line if diagonal movements are not allowed
}
if (col - 1 >= 0) { // Y坐标未出左界
checkNode(currentNode, col - 1, lowerRow, getDiagonalCost()); // Comment this line if diagonal movements are not allowed
}
checkNode(currentNode, col, lowerRow, getHvCost());
}
}
private void addAdjacentMiddleRow(Node currentNode) {
int row = currentNode.getRow();
int col = currentNode.getCol();
if (col - 1 >= 0) {
checkNode(currentNode, col - 1, row, getHvCost());
}
if (col + 1 < getSearchArea()[0].length) {
checkNode(currentNode, col + 1, row, getHvCost());
}
}
private void addAdjacentUpperRow(Node currentNode) { // 加入当前结点的下一行的相邻结点到开启列表
int row = currentNode.getRow(); // X坐标
int col = currentNode.getCol(); // Y坐标
int upperRow = row - 1; // X坐标减一得到新X坐标
if (upperRow >= 0) { // 新X坐标大于零(即未出左界)
if (col - 1 >= 0) { // Y坐标未出左界
checkNode(currentNode, col - 1, upperRow, getDiagonalCost()); // Comment this if diagonal movements are not allowed
} //检查结点(传参当前结点,相邻结点Y,相邻结点X,获得打斜花费(14)
if (col + 1 < getSearchArea()[0].length) { // Y坐标未出右界
checkNode(currentNode, col + 1, upperRow, getDiagonalCost()); // Comment this if diagonal movements are not allowed
} //检查结点(传参当前结点,相邻结点Y,相邻结点X,获得打斜花费(14)
checkNode(currentNode, col, upperRow, getHvCost()); //检查结点(传参当前结点,相邻结点Y,相邻结点X,获得横竖花费(10)
}
}
private void checkNode(Node currentNode, int col, int row, int cost) { // 检查新结点
Node adjacentNode = getSearchArea()[row][col]; // 获取地图中的相邻新点
if (!adjacentNode.isBlock() && !getClosedSet().contains(adjacentNode)) { // 非阻塞且关闭列表不包含相邻新点
if (!getOpenList().contains(adjacentNode)) { // 开启列表不包含相邻新点
adjacentNode.setNodeData(currentNode, cost); // 设置结点数据
getOpenList().add(adjacentNode); // 开启列表加入本结点
} else { // 如果是阻塞的或者关闭列表已经存在这个新点
boolean changed = adjacentNode.checkBetterPath(currentNode, cost); // 检查当前点是否再次进入开启列表
if (changed) {
// Remove and Add the changed node, so that the PriorityQueue can sort again its
// contents with the modified "finalCost" value of the modified node
getOpenList().remove(adjacentNode); // 开放列表移出新点
getOpenList().add(adjacentNode); // 开启列表加入新点
}
}
}
}
private boolean isFinalNode(Node currentNode) {
return currentNode.equals(finalNode);
}
private boolean isEmpty(PriorityQueue openList) {
return openList.size() == 0;
}
public void setBlock(int row, int col) {
this.searchArea[row][col].setBlock(true);
}
public void setInitialNode(Node initialNode) {
this.initialNode = initialNode;
}
public Node getFinalNode() {
return finalNode;
}
public void setFinalNode(Node finalNode) {
this.finalNode = finalNode;
}
private Node[][] getSearchArea() {
return searchArea;
}
public PriorityQueue getOpenList() {
return openList;
}
public Set getClosedSet() {
return closedSet;
}
public int getHvCost() {
return hvCost;
}
private int getDiagonalCost() {
return diagonalCost;
}
static class NodeComparator implements Comparator, Serializable {
private static final long serialVersionUID = 6106269076155338045L;
@Override
public int compare(Node node0, Node node1) {
return Integer.compare(node0.getFinalCost(), node1.getFinalCost());
}
}
}
package com.cj;
import java.util.Objects;
public class Node {
private int gCost; // G值
private int finalCost; // 总耗
private int heuristic; // 终点与当前点的曼哈顿距离
private int row; // 横坐标
private int col; // 纵坐标
private boolean isBlock; // 墙
private Node parent; // 父节点
public Node(int row, int col) {
super();
this.row = row;
this.col = col;
}
public void calculateHeuristic(Node finalNode) {
this.heuristic = Math.abs(finalNode.getRow() - getRow()) + Math.abs(finalNode.getCol() - getCol());
}
public void setNodeData(Node currentNode, int cost) {
int gCost = currentNode.getgCost() + cost; // 获得G花费(就是当前花费加上这一步的花费,即起点到当前结点的花费)
setParent(currentNode); // 设置父结点
setgCost(gCost); // 设置当前结点的G花费
calculateFinalCost(); // 计算总花费
}
public boolean checkBetterPath(Node currentNode, int cost) { // 传入新点与
int gCost = currentNode.getgCost() + cost; // 如果新点的G花配
if (gCost < getgCost()) { // 新G小于已有G
setNodeData(currentNode, cost); // 设置结点属性
return true; // 返回真
}
return false; // 返回假
}
private void calculateFinalCost() {
int finalCost = getgCost() + getHeuristic(); // 得到当前花费与启发式花费
setFinalCost(finalCost); // 设置最终花费
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Node node = (Node) obj;
return row == node.row && col == node.col;
}
@Override
public int hashCode() {
return Objects.hash(row, col);
}
@Override
public String toString() {
return "Node [row=" + row + ", col=" + col + "]";
}
public int getHeuristic() {
return heuristic;
}
public void setHeuristic(int heuristic) {
this.heuristic = heuristic;
}
public int getgCost() {
return gCost;
}
public void setgCost(int gCost) {
this.gCost = gCost;
}
public int getFinalCost() {
return finalCost;
}
public void setFinalCost(int finalCost) {
this.finalCost = finalCost;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public boolean isBlock() {
return isBlock;
}
public void setBlock(boolean isBlock) {
this.isBlock = isBlock;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
}