中国建设银行官网站,商业软文案例,微营销软件,网络营销计划包括哪七个步骤目录 正则表达式去除字符串前后空格#xff1a;去除每一行中首尾的空格去除开头的 数字_ 文件操作打印当前项目路径获取文件的上级目录/和\读取文件 内置包装类System类常用方法 Number类Integer类常用方法Float和Double 正则表达式
去除字符串前后空格#xff1a;
str.tri… 目录 正则表达式去除字符串前后空格去除每一行中首尾的空格去除开头的 数字_ 文件操作打印当前项目路径获取文件的上级目录/和\读取文件 内置包装类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(StreamString stream Files.lines(Paths.get(filePath), c)) {ListString 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 i0;iobj.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)此方法要求srcIndexlength srcArray.length且destIndexlength destArray.length 如果目标数组存在不会重构相当于替换部分元素 System.arraycopy(scores,0,newScores,2,8); 这里表示使用scores数组的[0,8)替换newScores数组的[2, 10) 获取当前的计算机时间
long startSystem.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 integer1new Integer(100); //以 int 型变量作为参数创建 Integer 对象 Integer integer2new 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); // 32Double的常用方法与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); // 64Boolean和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); // 8Character类 包含一个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 characternew Character(A);
int result1character.compareTo(new Character(V));
System.out.println(result1); // 输出-21
int result2character.compareTo(new Character(B));
System.out.println(result2); //输出-1
int result3character.compareTo(new Character(1));
System.out.println(result3); //输出16