2018-05-31 第二十五天
一、File練習(xí)
package?com.javase.day17;
import?java.io.File;
import?java.io.IOException;
import?java.text.ParseException;
import?java.text.SimpleDateFormat;
import?java.util.Date;
public?class?TestFile {
// 在相對(duì)路徑中創(chuàng)建1.txt 文件 工程目錄下
// 在c盤的根目錄下創(chuàng)建文件2.txt, 并修改其修改的時(shí)間 1970-8-9。
// 在c盤的根目錄下創(chuàng)建文件c:/7/7/2.txt
// 將指定的目錄下的,指定的后綴的文件全部刪除
// 使用遞歸將某個(gè)目錄下的所有的文件以及子目錄中的文件 文件的個(gè)數(shù)的統(tǒng)計(jì)。
// 使用遞歸實(shí)現(xiàn),將指定的目錄以及其子目錄全部刪除。
public?static?void?main(String[] args) {
// 從當(dāng)前系統(tǒng)動(dòng)態(tài)獲得的路徑分隔符 和 目錄分隔符
System.out.println(File.pathSeparator);// ;
System.out.println(File.separator);// \
// deleteFilesBySuffix("c:\\class\\501", "class");
test2();
test3();
Integer.parseInt("21");
}
// 在res 的相對(duì)目錄中,創(chuàng)建 或者刪除指定的文件
static?void?test1() {
File file =?new?File("."?+ File.separator?+ "res"?+ File.separator
+ "1.txt");
if?(file.exists()) {// 文件存在,刪除,
file.delete();
}?else?{// 不存在,創(chuàng)建新的
try?{
file.createNewFile();
}?catch?(IOException e) {
e.printStackTrace();
}
}
}
// 在c盤的根目錄下創(chuàng)建文件2.txt, 并修改其修改的時(shí)間 1970-8-9。
static?void?test2() {
File file =?new?File("F:\\2.txt");
if?(!file.exists()) {
try?{
file.createNewFile();
}?catch?(IOException e) {
e.printStackTrace();
}
}
// 修改 time 要求是 距離 1970 年1月1日 的毫秒的時(shí)間差
try?{
file.setLastModified(Long
.parseLong(dateToStamp("1970-05-20 13:14:20")));
}?catch?(NumberFormatException e) {
e.printStackTrace();
}?catch?(ParseException e) {
e.printStackTrace();
}
// file.lastModified()
}
// 在c盤的根目錄下創(chuàng)建文件c:/7/7/2.txt
static?void?test3() {
File file =?new?File("F:\\88\\88");
// 先創(chuàng)建目錄
// file.mkdir() 只能創(chuàng)建一層
file.mkdirs();
// 再創(chuàng)建文件
File file2 =?new?File(file, "2.txt");
if?(!file2.exists()) {
try?{
file2.createNewFile();
}?catch?(IOException e) {
e.printStackTrace();
}
}
}
/**
*刪除指定目錄下的指定的后綴的文件
?*
?*?@param?path
?*?@param?suffix
?*/
static?void?deleteFilesBySuffix(String path, String suffix) {
File file =?new?File(path);
// 得到當(dāng)前目錄下的所有的文件
File[] files = file.listFiles();
// 遍歷所有的文件,如果文件的名字以suffix 結(jié)尾 就刪除
for?(File file2 : files) {
// 獲得文件的名字
String name = file2.getName();
if?(name.endsWith(suffix)) {
file2.delete();
}
}
}
static?int?counter;
// 使用遞歸將某個(gè)目錄下的所有的文件以及子目錄中的文件 文件的個(gè)數(shù)的統(tǒng)計(jì)。
static?void?countDirFile(File file) {
if?(file.isFile()) {// file 是文件
counter++;
}?else?{// file 是目錄
// 得到file 目錄下的所有的文件夾和文件的列表
File[] files = file.listFiles();
// 遍歷 使用同樣的方式處理每一個(gè)文件對(duì)象
for?(File file2 : files) {
countDirFile(file2);
}
}
}
// 使用遞歸實(shí)現(xiàn),將指定的目錄以及其子目錄全部刪除。
// 使用程序刪除一個(gè)目錄,必須把目錄中的內(nèi)容清空才能刪除,只能刪除空目錄。
static?void?delDirectroy(File file) {
if?(file.isFile()) {// file 是文件
file.delete();
}?else?{// file 是目錄
// 得到file 目錄下的所有的文件夾和文件的列表
File[] files = file.listFiles();
// 遍歷 使用同樣的方式處理每一個(gè)文件對(duì)象
for?(File file2 : files) {
delDirectroy(file2);
}
// 刪除當(dāng)前空目錄
file.delete();
}
}
/*
?* 將時(shí)間轉(zhuǎn)換為時(shí)間戳
?*/
public?static?String dateToStamp(String s)?throws?ParseException {
String res;
SimpleDateFormat simpleDateFormat =?new?SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long?ts = date.getTime();
res = String.valueOf(ts);
return?res;
}
}
二、Random
java.util.Random
用于獲得隨機(jī)數(shù)的。
boolean nextBoolean()
返回下一個(gè)偽隨機(jī)數(shù),它是取自此隨機(jī)數(shù)生成器序列的均勻分布的boolean 值。
int nextInt(int n)
返回一個(gè)偽隨機(jī)數(shù),它是取自此隨機(jī)數(shù)生成器序列的、在0(包括)和指定值(不包括)之間均勻分布的 int 值。
三、Runtime
Runtime:運(yùn)行時(shí),是一個(gè)單例類。
public class Runtimeextends Object每個(gè) Java 應(yīng)用程序都有一個(gè) Runtime 類實(shí)例,使應(yīng)用程序能夠與其運(yùn)行的環(huán)境相連接。可以通過 getRuntime 方法獲取當(dāng)前運(yùn)行時(shí)。
應(yīng)用程序不能創(chuàng)建自己的Runtime 類實(shí)例。
import?java.io.IOException;
public?class?TestRuntime {
public?static?void?main(String[] args)?throws?IOException {
//獲得這個(gè)Runtime 的唯一的實(shí)例
Runtime runtime?= Runtime.getRuntime();
//常用方法
runtime.freeMemory();
runtime.totalMemory();
//等價(jià)的gc?調(diào)用
runtime.gc();
System.gc();
//等價(jià)?結(jié)束?jvm
//System.exit(0);
//runtime.exit(0);
//用于執(zhí)行本地的程序
//runtime.exec("C:\\Program Files (x86)\\Tencent\\QQ\\Bin\\QQScLauncher.exe");
//使用指定的程序打開指定的文件??
//運(yùn)行了一個(gè)程序??Process 運(yùn)行程序?qū)ο蟆?/p>
Process process?= runtime.exec("notepad.exe ?src/com/bjsxt/normalclass/TestRuntime.java");
try?{
//程序在此等候3秒
Thread.sleep(3000);
}?catch?(InterruptedException e) {
e.printStackTrace();
}
//殺死進(jìn)程
process.destroy();
}
}
四、枚舉Enum
一種特殊的類類型。
所有的枚舉類都默認(rèn)繼承了父類Enum ,其中定義了一些方法,并重寫了toString。
每個(gè)枚舉的靜態(tài)的成員,都分配了一個(gè)默認(rèn)的值。通過成員調(diào)用ordinal()?方法獲得。第一個(gè)成員的值為0,依次遞增。
???枚舉類中的元素是無法通過下標(biāo)值來訪問的,如果你想指定訪問枚舉類中的某個(gè)值,你只能直接寫出它們的值,除此之外,別無他法。但是枚舉類有一個(gè)values()方法,這個(gè)方法可以將枚舉類轉(zhuǎn)換成一個(gè)枚舉類型的數(shù)組,轉(zhuǎn)換成數(shù)組之后我們就可以通過下標(biāo)來訪問我們的枚舉類中的值。
枚舉使用的范圍:
如果某些變量的值的范圍是固定的,不想被隨意破壞的,可以將該變量的類型定義為枚舉類型。
import?com.bjsxt.util.MyUtil;
public?class?TestEnum {
public?static?void?main(String[] args) {
Student student?=?new?Student("如花", 30, Gender.male);
System.out.println(student);
System.out.println(Gender.male.ordinal());
System.out.println(Gender.female.ordinal());
//獲得當(dāng)前枚舉類的所有的實(shí)例成員。
Gender[] values?= Gender.values();
//隨機(jī)打印星期的某一天
System.out.println(Week.values()[MyUtil.getRandomNumber(0, Week.values().length)]);
//枚舉在switch 中的用法
Week week?= Week.values()[MyUtil.getRandomNumber(0, Week.values().length)];
switch(week){
case?星期一:
break;
}
}
}
class?Student{
private?String name;
private?int?age;
private?Gender gender;
Student(String name,?int?age, Gender gender) {
super();
this.name?= name;
this.age?= age;
this.gender?= gender;
}
@Override
public?String toString() {
return?"Student [name="?+ name?+ ", age="?+ age?+ ", gender="?+ gender?+ "]";
}
}
enum?Gender{
//是final 的?static 的public 的成員?本類的實(shí)例。
male,female
}
enum?Week{
星期一,星期二,星期三,星期四,星期五,星期六,星期日
}
五、Date
字母日期或時(shí)間元素表示示例
G ? Era 標(biāo)志符 ?Text ?AD ?
y ? 年Year ?1996; 96
M ? 年中的月份Month ?July; Jul; 07
w ? 年中的周數(shù)Number ?27
W ? 月份中的周數(shù)Number ?2
D ? 年中的天數(shù)Number ?189
d ? 月份中的天數(shù)Number ?10
F ? 月份中的星期Number ?2
E ? 星期中的天數(shù)Text ?Tuesday; Tue
a ? Am/pm 標(biāo)記 ?Text ?PM ?
H ? 一天中的小時(shí)數(shù)(0-23) ?Number ?0
k 一天中的小時(shí)數(shù)(1-24) ?Number ?24
K ?am/pm 中的小時(shí)數(shù)(0-11) ?Number ?0 ?
h ? am/pm 中的小時(shí)數(shù)(1-12) ?Number ?12 ?
m 小時(shí)中的分鐘數(shù)Number ?30
s 分鐘中的秒數(shù)Number ?55
S 毫秒數(shù)Number ?978
z 時(shí)區(qū)General time zone ?Pacific Standard Time; PST; GMT-08:00
Z ? 時(shí)區(qū)RFC 822 time zone ?-0800
import?java.text.DateFormat;
import?java.text.ParseException;
import?java.text.SimpleDateFormat;
import?java.util.Date;
//關(guān)于日期的處理
//1:獲得日期的具體的信息
//2:XXXX年XX月XX日?XX時(shí)XX分XX秒??日期的格式化打印
//3:對(duì)于指定格式的日期的字符串,生成一個(gè)日期對(duì)象
public?class?TestDate {
public?static?void?main(String[] args)?throws?ParseException {
Date date?=?new?Date();
//常用方法
System.out.println(date.toString());
//本地的字符串形式
System.out.println(date.toLocaleString());
//獲得當(dāng)前時(shí)間
System.out.println(date.getTime());
System.out.println(System.currentTimeMillis());
//獲得各個(gè)時(shí)間的字段的值
System.out.println(date.getYear());//118 ?距離1900年的時(shí)間差
System.out.println(date.getDate());//日期?31
System.out.println(date.getMonth());//4 java 中的月份是0 序?[0-11]
System.out.println(date.getDay());// 4星期幾?java 的中星期也是0 序??美國的星期日是一個(gè)星期的第一天
System.out.println(date.getHours());//11
System.out.println(date.getMinutes());//15
System.out.println(date.getSeconds());//39
System.out.println(date.getTimezoneOffset());//東八區(qū)?-480分鐘
//修改日期的值。
//date.setDate(date);
//判斷某個(gè)日期是否在某個(gè)日期之前或者之后
System.out.println(date.before(new?Date()));
System.out.println(date.after(new?Date()));
//實(shí)現(xiàn)了內(nèi)部比較器
//date.compareTo(anotherDate)
//日期的格式化顯式??XXXX年XX月XX日?XX時(shí)XX分XX秒
System.out.println(date.toLocaleString());
//使用日期格式化對(duì)象對(duì)日期對(duì)象進(jìn)行格式化??
//pattern 定義的模式??格式
DateFormat format?=?new?SimpleDateFormat("yyyy/MM/dd HH-mm-ss");
//格式化日期對(duì)象
String dateStr?= format.format(date);
System.out.println(dateStr);
//對(duì)指定的日期字符串生成一個(gè)日期對(duì)象。
dateStr?= "2018-5-31 11:27:27";
format?=?new?SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date2?= format.parse(dateStr);
System.out.println(date2.toString());
}
}
六、Calendar 日歷類
import?java.util.Calendar;
public?class?TestCalendar {
public?static?void?main(String[] args) {
//創(chuàng)建?Calendar 對(duì)象
Calendar calendar?= Calendar.getInstance();
//calendar 是Date 的替代類,功能更加的強(qiáng)大。
System.out.println(calendar);
//獲得指定字段的值
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH));
System.out.println(calendar.get(Calendar.DATE));
System.out.println(calendar.get(Calendar.HOUR));
System.out.println(calendar.get(Calendar.MINUTE));
System.out.println(calendar.get(Calendar.SECOND));
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//31
System.out.println(calendar.get(Calendar.DAY_OF_WEEK));//5 一個(gè)星期的第幾天??從1開始算
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH));//一個(gè)月的第幾個(gè)星期
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));//過了幾周的時(shí)間了。小于等于上面的
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));//一年中的第幾周
System.out.println(Calendar.HOUR_OF_DAY);
// 修改時(shí)間
// calendar.set(field, value);
calendar.add(Calendar.YEAR, 2);
//本地的一周的第一天是星期幾
System.out.println(calendar.getFirstDayOfWeek());
}
}
七、集合
1:collection framework。 集合框架。
2:學(xué)習(xí)各種不同功能和特點(diǎn)的容器類。
3:數(shù)組也可以看作一種簡(jiǎn)單的容器。元素要求連續(xù),元素有序號(hào)。數(shù)組這種容器有很多的缺點(diǎn)。
java 提供了一整套的解決的方案。
整體了解一下:
1:序列 Sequence :容器中的對(duì)象是有序號(hào)的,有序的(添加的順序)。
2:集 Set :這樣的容器中對(duì)象是無序的,唯一的。
3:映射 Map : 一個(gè)對(duì)象是一組數(shù)據(jù) key--value即鍵--值--鍵值對(duì)兒??梢酝ㄟ^key 值找到和key對(duì)應(yīng)的value 的值。
Collection: 頂層的接口。定義了一系列的規(guī)范。 描述的容器的某些功能。功能都是針對(duì)容器中的元素制定的。
Collection 中定義的功能 對(duì) 容器中元素的要求 是: 無序的(沒有順序添加,Collection 中沒有提供根據(jù)序號(hào)添加和訪問元素的方法),不唯一的(對(duì)子類的實(shí)現(xiàn)往容器中添加元素的時(shí)候,可以添加重復(fù)的元素)。
Collection 容器的特點(diǎn): 無序,不唯一。
---List
---Set
八、List -- ArrayList
List 是 Collection 直接的子接口。
List 特點(diǎn): 元素有序(元素添加的序號(hào)),不唯一。
List的實(shí)現(xiàn)的子類之一 ArrayList。
ArrayList 特點(diǎn):元素有序(添加插入的順序),不唯一,元素內(nèi)容可以是 null.
import java.util.ArrayList;
import java.util.Iterator;
public class TestArrayList {
public static void main(String[] args) {
//創(chuàng)建對(duì)象。
//自動(dòng)類型推斷
ArrayList list = new ArrayList<>();
//增
// list.add(1);
// list.add("abcd");
// list.add("abcd");
list.add(new Student(12,"小明"));
//插入 ?元素序號(hào)0 序
// list.add(0, "efgh");
// list.add(3, "3456");
list.add(new Student(10,"小剛"));
//不能跳著插入元素
// list.add(7, "7891");
//刪
list.remove(1);
list.remove(new String("abcd"));
//記得重寫equals 方法
list.remove(new Student(10,"小剛"));
//改
// list.set(1, "7890");
//查
System.out.println(list.get(2));
//其他的方法
list.size();//元素的個(gè)數(shù)
list.isEmpty();//是否為空
list.contains(7890);//是否包含指定的元素
list.clear();//清空元素
// list.lastIndexOf(o)
// list.ensureCapacity(minCapacity); 擴(kuò)容
// list.trimToSize(); ?去掉空白元素
//遍歷容器的元素
//for ?使用基本的for 循環(huán)實(shí)現(xiàn)遍歷元素
int size = list.size();
for(int i=0;i< size;i++){
Student stu = list.get(i);
//TODO
System.out.println(stu);
}
// 使用foreach 遍歷元素 ???底層使用迭代器實(shí)現(xiàn)。
for (Student s : list) {
//TODO
System.out.println(s);
}
//使用迭代器
//得到容器對(duì)象上的迭代器對(duì)象
//指明迭代器迭代的元素的類型。
Iterator iterator = list.iterator();
while(iterator.hasNext()){
Student next = iterator.next();
System.out.println(next);
//next
}
System.out.println(list);
}
}
class Student{
private int age;
private String name;
Student(int age, String name) {
super();
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + "]";
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
九、迭代器
增強(qiáng)的for 循環(huán):遍歷容器底層使用迭代器實(shí)現(xiàn)。
十、泛型:
又稱為參數(shù)化類型---generic type。
泛型給容器類帶來的好處:
1:避免了因?yàn)檎`操作而在某種容器中添加了不希望添加的類型的元素。 控制容器元素的類型單一。
2:避免了在訪問容器元素的過程中,將Object 對(duì)象 強(qiáng)制轉(zhuǎn)換為 需要的類型。
ArrayList list?=?new?ArrayList<>();
Iterator iterator?= list.iterator();
while(iterator.hasNext()){
Student next?= iterator.next();
System.out.println(next);
//next
}
1:ArrayList 底層使用 Object[] elementData 數(shù)組實(shí)現(xiàn)。
2: 初始化容量為10。
3:擴(kuò)容的規(guī)則,10-->15--->22 每次擴(kuò)容的元素的個(gè)數(shù)為 現(xiàn)有容量的 50%。
優(yōu)點(diǎn):
1:通過下標(biāo)遍歷效率很快。
2:所有對(duì)數(shù)組元素的操作都提供了封裝的方法,使用很方便。
3:可以實(shí)現(xiàn)自動(dòng)擴(kuò)容。
缺點(diǎn):
1:根據(jù)內(nèi)容查找元素效率相對(duì)較低。
2:刪除,插入元素,因?yàn)樾枰苿?dòng)大量的元素,所以效率比較低。
總結(jié):ArrayList是一個(gè)性能很優(yōu)良的容器類。使用率很高。
?
例:
import?java.util.ArrayList;
import?java.util.Iterator;
import?java.util.List;
import?com.bjsxt.util.MyUtil;
public?class?TestArrayList1 {
public?static?void?main(String[] args) {
List list??=new?ArrayList<>();
for(int?i=0;i<20;i++){
list.add(Integer.toString(MyUtil.getRandomNumber(4, 7)));
}
list.add(null);
System.out.println(list);
//刪除內(nèi)容為?4 的字符串。
// int?size = list.size();
// for(int?i=0;i
// if("4".equals(list.get(i))){
// list.remove(i);
// i--;
// }
// }
System.out.println(list);
//獲得容器對(duì)象上的迭代器
Iterator iterator?= list.iterator();
while?(iterator.hasNext()) {
String str?= iterator.next();
if("4".equals(str)){
//在使用迭代器遍歷元素的過程中,不要使用集合容器對(duì)象去修改元素,不要?jiǎng)h除,添加元素。
//訪問可以,使用迭代器自身的remove?方法沒有問題。
list.remove(str);
}
}
System.out.println(list);
}
}
?
總結(jié)
以上是生活随笔為你收集整理的2018-05-31 第二十五天的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 楚留香云梦2.0捏脸数据
- 下一篇: i7 7700hq是什么水平