Java学习--day7

来源:互联网 发布:前程无忧和前锦网络 编辑:程序博客网 时间:2024/06/03 11:09
Java常用类库
1、StringBuffer类
String类不能改变内容,只能改变字符串的地址。频繁修改字符串就要用到StringBuffer类,append()方法追加内容。
StringBuffer类大部分方法和String类相同。

字符串连接:append()
StringBuffer buf = new StringBuffer();
buf.append("hello").append("world");
任意位置添加内容:insert()
buf(0,"666");
字符串反转:reverse()
String ss=buf.reverse().toString();
替换指定范围的内容:replace() (注意,在String类中替换是使用 )
buf.replace(6,11,"zhao");
字符串截取:substring()
String str = buf.substring(6,15);
删除指定范围字符串:delete()
String ss=buf.delete(6,15).toString();
查找指定内容是否存在:indexOf()
buf.indexOf("hello"); //存在则返回位置,不存在返回-1
2、Runtime类
Runtime类表示运行时操作类,是一个封装了JVM进程的类,每一个JVM对应一个Runtime类的实例。
获取实例方法:
Runtime run = Runtime.getRuntime();
run.maxMemory();最大内存量
run.freeMemory();空闲内存
run.gc();运行垃圾收集
run.exec("notepad.exe");运行记事本程序。返回值是一个process,表示一个操作系统的进程类
所以可以用Process pro =null;pro=run.exec("notepad.exe");进行接收。再通过Thread.sleep(5000);进行控制
3、国际化程序
国际化的操作就是指一个程序可以同时适应多门语言。实现要求:
①Locale类
②属性文件(也称资源文件,后缀为.properties的文件,内容结构为key==value的形式)
③ResourceBundle类,用来访问这些属性文件
④MessageFormat类,格式化属性文件的占位字符串
操作流程:通过Locale类指定区域码,然后ResourceBundle根据这个区域码找到对应的属性文件,如果文件中存在动态文本,则使用MessageFormat类格式化

动态文本例:(由外部设置占位符的内容)
英文文件名 Message_en_US.properties--------info=hello,{0}
中文文件名 Message_zh_CN.properties--------info=\u4f60\u597d\uff0c{0}\uff01(要将中文转换成Unicode编码)
法语文件名 Message_fr_FR.properties--------info=Bonjour,{0}
Locale zhLoc = new Locale("zh","CN");
ResourceBundle zhrb = ResourceBundle.getBundle("Message",zhLoc);
String str1 = zhrb.getString("info");
System.out.println("中文:" + MessageFormat.format(str1,"赵"));

当然也可以用类代替资源文件(必须继承ListResourceBundle类,然后覆写getContent()方法),比较少用
4、System类
System类是一些与系统相关属性和方法的集合,并且System类中所有属性都是静态的,可以直接用System类调用。
System.currentTimeMillis() 返回以毫秒为单位的当前世界
System.getProperties()取得本机全部环境属性
System.getProperty("")取得某个指定属性
部分属性说明:①文件默认编码file.encoding=GBK②文件分隔符file.separate=\
5、垃圾回收
一个对象若不再被任何栈对象所引用,则被称为垃圾对象,等待被回收。
Java有自己的的垃圾自动收集机制。System类也有gc()方法,是对Runtime类gc()方法的封装。
开发中垃圾收集一般都由系统自动完成。
一个对象如果被回收前需要进行某些操作,就要覆写finalize() throws Throwable方法。
6、日期操作类
Date类(输出比较单一)
Date date=new Date();sysout(date);(这种方式输出全部时间信息,无法指定内容输出)
Calendar类(代码比较复杂)
Calendar calendar = new GregorianCalendar();
calendar.get(Calendar.YERN); //取得年
calendar.get(Calendar.MONTH); //取得月
.....
DateFormat类
DateFormat是一个抽象类,无法直接实例化。dam提供了一个静态方法getDateInstance()本类取得实例。
DateFormat df1=DateFormat.getDateInstance();sysout(df1.format(new Date()));2017-3-22
DateFormat df1=DateFormat.getDateTimeInstance();sysout(df2.format(new Date()));2017-3-22 12:24:53
若要指定显示风格DateFormat df1=DateFormat.getDateInstance(DateFormat.YEAR_FIELD,new Locale("zh","CN"));...//2017年3月22日
SimpleDateFormat类(开发经常用于将String变为Date型数据)
1)简单的格式化
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
System.out.println(sdf1.format(new Date()));
2017年03月22日
2)取出第一个字符串模板的内容放到第二个中
String strDate="2017-03-22";
String part1="yyyy-MM-dd" //模板1
SimpleDateFormat sdf1 = new SimpleDateFormat(part1);
SimpleDateFormat sdf2 = new SimpleDateFormat(yyyy年MM月dd日);//实例化模板
Date d = null;
try{
d=sdf1.parse(strDate);//取出日期
}catch(ParseException e){....}
sysout(sdf2.format(d));//2017年03月22日
7、MAth类
基本方法
sqrt()平方根
max()main()最大最小值
pow()几次方
round()四舍五入,将小数点后面都忽略。如果要精确需要使用BigDecimal类
8、Random类
Random r = new Random();r.nextInt(100) //生成一个不大于100的数
9、NumberFormat类
数字格式化
NumberFormat nf = NumberFormat.getInstance();
nf.format(1000000); //输出1,000,000
DecimalFormat类(格式化数字,但比NumberFormat方便)
DecimalFormat df = new DecimalFormat(###,###.###);
sysout(df.format(111222.34567)); //111,222.346
10、BigInteger类
当整数大于long时,就需要用到。
常用方法①加法add()②减法subtract()③乘法multiply()④除法divide()⑤max(),min()⑥商和余数divideAndRemainder()
11、BigDecimal类
需要准确的计算结果时用到,也可以进行大数操作
12、Arrays类
Arrays.sort()数组排序
Arrays.binarySearch()返回某数组中某个元素的位置
Arrays.toString 内容以String类型输出
13、Comparable接口
Comparable接口
public interface Comparable<T>{
public int compareTo(T o);
}
若要使用比较器进行排序,需要实现Comparable接口,并覆写 compareTo方法
14、另一种比较器Comparator
public interface Comparator<T>{
public int compare(T o1, T o2);
boolean equals(Object obj);
}
15、观察者模式
类似watcher,监听器
需要被观察的类必须继承Observable类,每个观察者需要实现Observer接口覆写update()方法
16、正则表达式
使用Pattern类(进行正则规范)和Matcher类(执行规范,验证)
Pattern p = Pattern.compile("\\d{4}-\\d{2}");
Matcher m = p.Matcher("2017-03");
if(m.matches()){
sysout("合法");
}
但由于String类对正则表达式的支持,所以最好使用String类中的方法。
boolean temp = "2017-03-22".matches("\\d{4}-\\d{2}-\\d{2}");
返回true
17、定时调度
Timer类是一种线程设施,可以实现在某一个时间或者时间段后安排某一个任务执行一次或定期重复执行。该功能要和TimeTask类配合使用。TimeTask类用来实现由Timer安排的一次或重复执行的某个任务。每个Timer对象对应一个线程。
TimeTask类是抽象类,需要建立类来继承此类,并实现抽象方法。
流程:
写一个MyTask类继承TimeTask类,覆写run()方法。
主类中:Timer t =new Timer();
MyTask mytask = new MyTask();
t.schedule(mytask ,1000,2000); //设置任务的执行,1秒后开始,每2秒重复

0 0