常用类:
Math: 数学方法, PI
Random , SecureRandom :随机数, 安全随机数
Date :日期
1.自动化脚本写日志,一般要把时间戳写到日志文件中,方便定位问题
2.自动化脚本有定时任务的场景,设置定时任务时不能设置死的时间.
获取当前时间后,往后延迟几分钟,作为定时任务的启动时间
3.定位性能问题,在进出函数的时候打印时间,计算函数执行耗时.System.out.println(Math.PI);
System.out.println(Math.pow(10, 2));
//Random random = new Random();//当前时间作为种子生成随机数
SecureRandom random = new SecureRandom();//sn
for (int i = 0; i < 10; i++) {
//随机生成int类型的数
System.out.print(random.nextInt() + " ");
}
for (int i = 0; i < 10; i++) {
//随机生成int类型的数,从0~bound之间产生随机数,包含下边界,不包含上边界
System.out.print(random.nextInt(10) + " ");
}
System.out.println();
for (int i = 0; i < 10; i++) {
//随机生成float类型的数,在0~1之间
System.out.print(random.nextFloat() + " ");
}
Date date = new Date();// 当前时间
System.out.println(date);//Fri Mar 12 09:56:14 CST 2021
//y年,yyyy表示年用4位数来表示
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
System.out.println(sdf.format(date));//2021年03月12日 10时00分51秒
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));//2021-03-12 10:00:51
//时间戳
long time = date.getTime();
//相对北京时间 1970年1月1日 8:00 过了多少毫秒
System.out.println(time);
Date date1 = new Date(time+300000);//给time加上5分钟
System.out.println(date1);
System.out.println(sdf.format(date1));
long begin = (new Date()).getTime();
func();
long end = (new Date()).getTime();
System.out.println("执行func耗时"+(end-begin));常用类
String :字符串相关的操作
StringBuilder :+连接效率低 ,大量的字符串连接,用StringBuilder来实现
StringJoiner :join方法的升级版文件的读写
按字节读写文件,一次处理一个字节 InputStream / OutputStream 是读写的父类
按字符读写文件,一次处理一个字符(两个字节) , 字符流 Reader/Writer 是读写的父类
读文件常用的类: 1.创建读文件的对象 2. 读文件 3. 关闭对象
FileInputStream
InputStreamReader
BufferedReader
写文件常用类:
FileOutputStream
OutputStreamWriter
BufferedWriterpublic static void read1(String path) throws IOException {
FileInputStream fs = new FileInputStream(path);
int temp = fs.read();
while(temp != -1){
System.out.write(temp);
temp = fs.read();
System.out.flush();
}
fs.close();
}
public static void read2(String path) throws IOException {
InputStream is = new FileInputStream(path);
//InputStreamReader需要一个InputStream的实例,InputStream是抽象类,用其子类实例化
InputStreamReader isr = new InputStreamReader(is);
int temp = isr.read(); //一次读一个字符
while(temp != -1){
System.out.print((char)temp);//将整数表示的字符转成char
temp = isr.read();
}
isr.close();
is.close();
}
public static void read3(String path) throws IOException {
Reader r = new FileReader(path);
BufferedReader br = new BufferedReader(r);
String temp = br.readLine();
while(temp != null){
System.out.println(temp);
temp = br.readLine();
}
br.close();
r.close();
}
|