- 使用Scanner类读取文件
public class ScannerFile{
public static void main(String[] args){
Scanner sc = new Scanner(new File("file.txt"))
while(sc.hasNextLine()){
System.out.println(sc.nextLine());
}
}
- System.currentTimeMillis() 方法获取时间
//获取的是距离1990-1-1时间差的毫秒值
String s = System.currentTimeMillis();
- Object类中的hashCode()方法可以根据地址值唯一的确认一个对象;
String类重写了hashCode()方法,只要字符序列相同则hashCode的值就相同;
使用identityHashCode()方法可以唯一的标识对象;
String、StringBuffer、StringBuilder详解
1.三者的区别;
String是不可变量(栈对象可以指向多个堆对象),其他两者是可变量。
2.StringBuffe和StringBuilder的区别;
二者基本类似,只是StringBuffer的线程安全。所以在开发中多用SringBuilder;
3.String类常用的方法总结;
3.1 字符串和字符串数组的相互转换
//将字符串转为数组
String s = new string("1234")
char[] chars = new char[s.length()];
chars = s.toCharArray();
//将字符数组转为字符串
char[] chars = new char[]{'1','2','3','4'};
String s = new String(chars);
3.2 String重写的equals比较;
String中的equals比较只比值域;
比较其他对象时,只有同一个对象才会返回true;
String s1 = new string("1234");
String s2 = new string("1234");
//返回true
s1.equals(s2);
- 使用BigDecimal方法解决浮点数精度丢失问题;
使用String value创建对象
//使用f1进行运算不会产生精度丢失问题;
BigDecimal f1 = new BigDecimal("0.05")
- Calendar类
//分别获取年月日时分秒
Calendar cal=Calendar.getInstance();
int y = cal.get(Calendar.YEAR);
int m = cal.get(Calendar.MONTH);
int d = cal.get(Calendar.DATE);
int h = cal.get(Calendar.HOUR_OF_DAY);
int mi = cal.get(Calendar.MINUTE);
int s = cal.get(Calendar.SECOND);
System.out.println("现在时刻是"+y+"年"+m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒");
//获取当前时间
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
System.out.println(dateFormat.format(calendar.getTime()));