文件类使用
做日志文件1
2
3
4
5
6
7
8
9
10
11
12
13File log = new File(System.getProperty("user.dir"),"liuzhihao.log") //System.getProperty("user.dir")获取当前用户目录
try{
if(!log.exists()){
log.createNewFile();
}else{
}
}catch(IOException e){
System.out.print(e);
}
FileWriter wri = null;
wri = new FileWriter(log,true);//需捕获IO异常
wri.wirte(new Date()+"已经开始了"+System.getProperties().getProperty("line.separator"))//用系统的换行
wri.flush()//正式写入
获取系统配置
1 | Properties pp = System.getProperties(); |
shell命令执行
1 | Runtime prt = Runtime.getRuntime(); |
获取系统显示器高宽
1 | Toolkit gTool = Toolkit.getDefaultToolkit(); |
调用系统浏览器
1 | Desktop.getDesktop().browse(new URI("http://www.baidu.com")); |
配置文件properties使用
使用properties类来实现,该类是继承HashTable的子类,因此Map结合的基本使用方法它都有,但是我们需要的是下面的:
1、getProperty(String key),用指定的键在此属性列表中搜索属性。也就是通过参数key得到key所对应的value。
2、public void load(Reader reader),从输入流中读取属性列表(键和元素对)。通过对指定文件进行装载来获取该文件中的所有键值对。以提供getProperty(String key)搜索。
3、setProperty(String key,String value),调用HashTable的方法put。只不过是将put(k KEY,V value)通过显式的指定为String类型保证了只可以接受String类型的参数,在实际开发时,我们也经常需要使用这种方法,来包装一个父类的方法。
4、public void store(Writer writer,String comments)与load方法相反,该方法将键值对写入到指定的文件中去。
5、public Set
获取年月日
Calendar类1
2
3
4
5
6
7
8
9
10
11
12
13public static String getTiem(){
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
int week = cal.get(Calendar.DAY_OF_WEEK);
String dateTime="";
dateTime=dateTime+year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second+" "+week;
return dateTime;
}
获得的月份要加1。