目录
- 正则表达式
 - 去除字符串前后空格:
 - 去除每一行中首尾的空格
 - 去除开头的 '数字_'
 
- 文件操作
 - 打印当前项目路径
 - 获取文件的上级目录
 - /和\
 - 读取文件
 
- 内置包装类
 - System类
 - 常用方法
 
- Number类
 - Integer类常用方法
 - Float和Double
 
正则表达式
去除字符串前后空格:
str.trim()
去除每一行中首尾的空格
import java.util.regex.*;
Pattern pt = Pattern.compile("^\\s*|\\s*$");
Matcher mt = pt.matcher(line);
String str = mt.replaceAll("");
 
去除开头的 ‘数字_’
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// 处理例如 '0_1_a_test.docx',只需要去掉最开始的 'idx_'
String str = "10_1_a_test.docx";
Pattern p = Pattern.compile("\\d*_"); //匹配一个或多个数字再加一个下划线
Matcher m = p.matcher(str);
String tempAttachName = m.replaceFirst(""); // 输出:1_a_test.docx
 
文件操作
打印当前项目路径
System.getProperty("user.dir")
 这是项目根目录路径,下面的自己写==
获取文件的上级目录
import java.io.File;
 
public class DirSize {
    public static void main(String[] args) {
        File dir = new File("/home/huanyu/Desktop");
        String parentPath =dir.getParent();
        System.out.println("该目录的上级目录为:"+parentPath);
    }
}
 
/和\
windows和Linux的文件分隔符不同,获取方式
import java.io.File;
public class test{
    public static void main(String args[]) {
        String a = System.getProperty("file.separator");
	System.out.println(a);
	System.out.println(File.separator);
    }
}
 
读取文件
FileInputStream fis = new FileInputStream(filePath); // filePath是自定义路径str
// 指定编码格式
BufferedReader br = new BufferedReader(new InputStreamReader(fis, "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
    //System.out.println(line);
    //break;
}
fis.close();
这个方便在于能快速处理:
//1. filter line 3
//2. convert all content to upper case
//3. convert it into a List
list = stream
   .filter(line -> !line.startsWith("line3"))
   .map(String::toUpperCase)
   .collect(Collectors.toList());
Charset c = Charset.forName("UTF-8");
//System.out.println(Charset.isSupported("UTF-8"));
try(Stream<String> stream = Files.lines(Paths.get(filePath), c)) {
    List<String> raws = new ArrayList<>();
    raws = stream.collect(Collectors.toList());
    for (String s:raws) {
        //System.out.println(s);
    }
} catch(IOException e) {
    e.printStackTrace();
}      
 
内置包装类
Object类是所有类的父类,如果一个类被定义后,没有指定继承的父类,那么默认父类就是Object类。因此Object类中定义的方法,其他类也可以用:如equals()和getClass()。
public static void printClassinfo(Object obj){
        //获取类名
        System.out.println("类名:"+obj.getClass().getName());
        //获取父类名
        System.out.println("父类:"+obj.getClass().getSuperclass().getName());
        System.out.println("实现的接口有:");
        //获取实现的接口并输出
        for(int i=0;i<obj.getClass().getInterfaces().length;i++)
        {
            System.out.println(obj.getClass().getInterfaces()[i] + "\n");
        }
    }
 
System类
构造方法是private,所以不能创建它的对象,不能实例化它。其内部的成员变量和方法都是static的,可以调用。成员变量有:
- PrintStream out 标准输出流,如println就是out的方法,不是System的方法
 - InputStream in 标准输入流
 - PrintStream err 标准错误输出流
 
public void contextLoads() {
		int c;
		try {
			// 使用System.in的话读汉字会出错,用InputStreamReader
			// 并指定编码集才可以
			c = System.in.read(); 
			//InputStreamReader in = new InputStreamReader(System.in, "UTF-8");
			//c = in.read();
			while (c != '\r') {
				System.out.print((char) c);
				//c = in.read();
				c = System.in.read(); 
			}
		} catch(IOException e) {
			System.out.println("\nThis is IOException: \n");
			System.out.println(e.toString());
		} finally {
			System.out.println("\nThis is finally bloc: \n");
			System.err.println();
		}
	}
 
常用方法
-  
数组复制
arraycopy方法:从原数组中截取一段,替换到目标数组中
System.arraycopy(dataType[] srcArray,int srcIndex,dataType[] destArray,int destIndex,int length)此方法要求srcIndex+length <= srcArray.length且destIndex+length <= destArray.length
如果目标数组存在,不会重构,相当于替换部分元素
System.arraycopy(scores,0,newScores,2,8);
这里表示使用scores数组的[0,8)替换newScores数组的[2, 10) -  
获取当前的计算机时间
 
long start=System.currentTimeMillis();
// code block
long end = System.currentTimeMillis();
double time=(end-start) / 1000.0;
 
因为获取的都是毫秒,要除以1000换算到秒
- 终止当前正在运行的Java虚拟机
 
public static void exit(int status)
 
status为0时表示正常退出,非0时是异常。在图形界面编程中实现程序的退出功能。
- 获得系统级的参数
 
System.getProperty(属性名);
        System.out.println(System.getProperty("java.version"));
        System.out.println(System.getProperty("java.home"));
        System.out.println(System.getProperty("os.name"));
        System.out.println(System.getProperty("os.version"));
        System.out.println(System.getProperty("user.name"));
        System.out.println(System.getProperty("user.home"));
        System.out.println(System.getProperty("user.dir")); 
 
Number类
Number类是一个抽象类,属于java.lang,数字类包装类都是Number的子类。它定义了一些抽象方法,以各种不通过数字格式返回对象的值,如xxxValue()方法,就是将Number对象转换为xxx数据类型的值返回。抽象类是不能直接实例化的,要实例化具体的子类:
Number num = new Double(12.5);
System.out.println("返回 int 类型的值:"+num.intValue());
 
各个子类中都有类似方法,替换类型即可
Integer类常用方法
Integer integer1=new Integer(100); //以 int 型变量作为参数创建 Integer 对象 Integer integer2=new Integer(“100”); //以 String 型变量作为参数创建 Integer 对象
 
Integer类的常量
 
Float和Double
Float类常用方法
 构造函数的参数可以带double,float,String
 
Float类常用常量(可以用反射去看)
getFields() getDeclareFields()
System.out.println(Float.MAX_VALUE); //3.4028235E38
System.out.println(Float.MIN_NORMAL); // 1.17549435E-38
System.out.println(Float.MIN_VALUE); // 1.4E-45
System.out.println(Float.MAX_EXPONENT); // 127
System.out.println(Float.MIN_EXPONENT); // -126
System.out.println(Float.SIZE); // 32
 
Double的常用方法与Float的类似,直接替换为Double即可
System.out.println(Double.MAX_VALUE); // 1.7976931348623157E308
System.out.println(Double.MIN_NORMAL); // 2.2250738585072014E-308
System.out.println(Double.MIN_VALUE); // 4.9E-324
System.out.println(Double.MAX_EXPONENT); // 1023
System.out.println(Double.MIN_EXPONENT); // -1022
System.out.println(Double.SIZE); // 64
 
Boolean和Byte
Boolean(boolean boolValue); 
Boolean(String boolString);
System.out.println(Byte.MAX_VALUE); // 127
System.out.println(Byte.MIN_VALUE); // -128
System.out.println(Byte.SIZE); // 8
 
Character类
 包含一个char。常用方法:
 is系列:isDigit(char ch) isLowerCase(char ch) isUpperCase(char ch)
 isLetter(int codePoint) isLetterOrDigit(int codePoint)
to系列:char toLowerCase(char ch) char toUpperCase(char ch)
另外,compareTo返回的是两个字符的标准代码差值,如
Character character=new Character('A');
int result1=character.compareTo(new Character('V'));
System.out.println(result1);    // 输出:-21
int result2=character.compareTo(new Character('B'));
System.out.println(result2);    //输出:-1
int result3=character.compareTo(new Character('1'));
System.out.println(result3);    //输出:16
                





![[Java基础揉碎]idea工具](https://img-blog.csdnimg.cn/direct/2c8af4aff1cf4140a545c13122051f01.png)








![命令执行 [网鼎杯 2020 朱雀组]Nmap1](https://img-blog.csdnimg.cn/direct/b10ca899c2124f93aa76875b6bc7f331.png)


