目录
一、容器概念
二、Collection 接口
1. Collection接口有两个子接口Set和List
2. Collection中常用方法:
3. 代码示范:
4. Collection的遍历
5. 泛型 < >
三、Iterator 接口
1. Iterator的特点
2. Iterator接口常用方法
3. 使用迭代器时的步骤
四、List接口
1. 特征
2. 常用方法
3. List的遍历
4. 解决并发增加问题
五、ArrayList类
1. 特点
2. ArrayList常用方法
3. ArrayList的遍历
4. ArrayList储存对象
六、LinkedList类
1. 特点
2. LinkedList遍历和常用方法
七、Set接口
1. 特点
2. Set常用方法
3. Set的遍历
八、HashSet类
1. 特点
2. 遍历
3. 常用方法
4. HashSet去重
5. 代码示范
九、Map接口
1. 特征
2. Map常用方法
3. Map的遍历
4. 引用类型数据去重
5. Properties类
十、Collections工具类
1. 特征
2. Collections常用方法
3. 代码示范
十一、File类
1. 概念
2. 要点
3. 常用方法
(1)创建功能
(2)重命名和删除功能
(3)判断功能
(4)获取功能
数组是一种简单的线性序列,可以快速的访问数组元素,效率高,但是从数组 长度一旦确定,不可改变,因此,数组远远不能满足我们的需求。 我们需要一种灵活的,容量可以随时扩充的容器来装载我们的对象,这就需要用到容器类,或者叫集合框架。
体系图如下:
方法摘要 | ||
---|---|---|
boolean |
add(E e) 确保此 collection 包含指定的元素(可选操作)。 |
|
boolean |
addAll(Collection extends E> c) 将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。 |
|
void |
clear() 移除此 collection 中的所有元素(可选操作)。 |
|
boolean |
contains(Object o) 如果此 collection 包含指定的元素,则返回 true。 |
|
boolean |
containsAll(Collection> c) 如果此 collection 包含指定 collection 中的所有元素,则返回 true。 |
|
boolean |
equals(Object o) 比较此 collection 与指定对象是否相等。 |
|
int |
hashCode() 返回此 collection 的哈希码值。 |
|
boolean |
isEmpty() 如果此 collection 不包含元素,则返回 true。 |
|
Iterator |
iterator() 返回在此 collection 的元素上进行迭代的迭代器。 |
|
boolean |
remove(Object o) 从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。 |
|
boolean |
removeAll(Collection> c) 移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。 |
|
boolean |
retainAll(Collection> c) 仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。 |
|
int |
size() 返回此 collection 中的元素数。 |
|
Object[] |
toArray() 返回包含此 collection 中所有元素的数组。 |
|
|
toArray(T[] a) 返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。 |
public static void main(String[] args) {
//1.创建容器
Collection c=new ArrayList();
/*
* 2.添加元素
* boolean add(E e)
确保此 collection 包含指定的元素(可选操作)。
boolean addAll(Collection extends E> c)
将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。
*/
c.add("清华大学");
c.add("北京大学");
Collection c2=new ArrayList();
c2.add("哈工大");
c2.add("佳木斯大学");
c2.add(1); //Integer 自动装箱
System.out.println(c.size());
c.addAll(c2);
/*
* 3.获取
* int size()
返回此 collection 中的元素数。
boolean isEmpty()
如果此 collection 不包含元素,则返回 true。
*/
System.out.println(c.size());
System.out.println("判断是否为空:"+c.isEmpty());
System.out.println(c);
/*
* 4.删除
* void clear()
移除此 collection 中的所有元素(可选操作)。
boolean remove(Object o)
从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
boolean removeAll(Collection> c)
移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
boolean retainAll(Collection> c)
仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。
*/
System.out.println(c.size());
System.out.println("remove:"+c.remove("清华大学"));
System.out.println(c.size());
//System.out.println("removeAll:"+c.removeAll(c2));
System.out.println("removeAll:"+c.retainAll(c2));
System.out.println(c.size());
//c.clear();
System.out.println(c.size());
System.out.println("判断是否为空:"+c.isEmpty());
/*
* Object[] toArray()
返回包含此 collection 中所有元素的数组。
*/
Object[] obj=c.toArray();
System.out.println(Arrays.toString(obj));
/*
* 5.是否包含
* boolean contains(Object o)
如果此 collection 包含指定的元素,则返回 true。
*/
System.out.println(c.contains("佳木斯大学"));
Collection因为没有索引去定位,因此普通for循环无法使用,可以使用增强for循环和迭代器来遍历它。
public static void main(String[] args) {
Collection c=new ArrayList();
c.add("拇指");
c.add("食指");
c.add("中指");
c.add("无名指");
c.add("小手指");
//foreach循环
for(Object s:c){
System.out.println(s);
}
System.out.println("----------------------------------");
//使用迭代器
//1.获取一个迭代器对象 凡是实现了Collection接口的类都有iterator()方法
Iterator it=c.iterator();
//2.判断是否还有下一个元素
while(it.hasNext()){
//3.如果有下一个元素就使用next()方法获取
//String s=(String) it.next();
Object s = it.next();
System.out.println(s);
}
}
所有实现了 Collection 接口的容器类个 都有一个 iterator 方法用以返回一个实现了 Iterator 接口的对象。Iterator 对象称作迭代器,用以方便的实现对容器内元素的遍历操作。
boolean hasNext(); //判断是否有元素没有被遍历
Object next(); //返回游标当前位置的元素并将游标移动到下一个位置
void remove(); //删除游标左面的元素
//使用迭代器
//1.获取一个迭代器对象
Iterator it=c.iterator();
//2.判断是否存在下一个元素
while(it.hasNext()){
//3.如果有下一个元素就使用next()方法获取
//String s=(String) it.next();
Object s = it.next();
System.out.println(s);
}
有序的,可重复的。
方法摘要 | ||
---|---|---|
boolean |
add(E e) 向列表的尾部添加指定的元素(可选操作)。 |
|
void |
add(int index, E element) 在列表的指定位置插入指定元素(可选操作)。 |
|
boolean |
addAll(Collection extends E> c) 添加指定 collection 中的所有元素到此列表的结尾,顺序是指定 collection 的迭代器返回这些元素的顺序(可选操作)。 |
|
boolean |
addAll(int index, Collection extends E> c) 将指定 collection 中的所有元素都插入到列表中的指定位置(可选操作)。 |
|
void |
clear() 从列表中移除所有元素(可选操作)。 |
|
boolean |
contains(Object o) 如果列表包含指定的元素,则返回 true。 |
|
boolean |
containsAll(Collection> c) 如果列表包含指定 collection 的所有元素,则返回 true。 |
|
boolean |
equals(Object o) 比较指定的对象与列表是否相等。 |
|
E |
get(int index) 返回列表中指定位置的元素。 |
|
int |
hashCode() 返回列表的哈希码值。 |
|
int |
indexOf(Object o) 返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。 |
|
boolean |
isEmpty() 如果列表不包含元素,则返回 true。 |
|
Iterator |
iterator() 返回按适当顺序在列表的元素上进行迭代的迭代器。 |
|
int |
lastIndexOf(Object o) 返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 -1。 |
|
ListIterator |
listIterator() 返回此列表元素的列表迭代器(按适当顺序)。 |
|
ListIterator |
listIterator(int index) 返回列表中元素的列表迭代器(按适当顺序),从列表的指定位置开始。 |
|
E |
remove(int index) 移除列表中指定位置的元素(可选操作)。 |
|
boolean |
remove(Object o) 从此列表中移除第一次出现的指定元素(如果存在)(可选操作)。 |
|
boolean |
removeAll(Collection> c) 从列表中移除指定 collection 中包含的其所有元素(可选操作)。 |
|
boolean |
retainAll(Collection> c) 仅在列表中保留指定 collection 中所包含的元素(可选操作)。 |
|
E |
set(int index, E element) 用指定元素替换列表中指定位置的元素(可选操作)。 |
|
int |
size() 返回列表中的元素数。 |
|
List |
subList(int fromIndex, int toIndex) 返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。 |
|
Object[] |
toArray() 返回按适当顺序包含列表中的所有元素的数组(从第一个元素到最后一个元素)。 |
|
|
toArray(T[] a) 返回按适当顺序(从第一个元素到最后一个元素)包含列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。 |
List因为是有序的,所以可以使用索引,因此我们有三种方式可以遍历它。
public static void main(String[] args) {
List ls=new ArrayList();
//增加
ls.add("钢铁侠");
ls.add("雷神");
ls.add("灭霸");
ls.add("美国队长");
//1--------------for--------------
for(int i=0;i
List在使用增强for和迭代器来遍历元素时,由于多个引用同时调用会引发多并发异常问题。
ConcurrentModificationException:方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。
解决并发增加问题:使用 ListIterator
//3--------------迭代器-------------- 底层是由foreach实现的
//ConcurrentModificationException
//1)获取迭代器对象
/* Iterator it=ls.iterator();
//2)判断是否存在下一个元素
while(it.hasNext()){
//3)获取
if(it.next().equals("灭霸")){
ls.add("惊奇队长");
}
}
System.out.println(ls);*/
//4.-----------解决并发增加问题:使用 ListIterator
//1.获取迭代器
ListIterator li=ls.listIterator();
//判断
while(li.hasNext()){
if(li.next().equals("灭霸")){
li.add("惊奇队长");
}
}
System.out.println(ls);
}
ArrayList 是 List 的子类,它和 HashSet 相反,允许存放重复元素,因此有序。
由于ArrayList是List的实现类,因此继承了List的所有方法,常用方法也基本相同。
同List接口的遍历方式相同
注意: ArrayList在储存对象时,使用indexOf方法时调用的equals方法比较的是地址,如果要比较内容需要重写equals方法,生成快捷键Alt+S.
public class ArrayListTest09{
public static void main(String[] args) {
List ls=new ArrayList();
ls.add(new Person("欢欢",18));
ls.add(new Person("书书",19));
System.out.println(ls.indexOf(new Person("书书",19))); //-1 因为Person中没有重写equals方法
List ls2=new ArrayList();
ls2.add(new String("么么"));
System.out.println(ls2.indexOf(new String("么么"))); // 0 因为String类型中重写了squals方法
}
}
class Person{
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//重写equals方法,比较内容
@Override
/*public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) 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;
}*/
}
LinkedList 可以重复,是一种可以在任何位置进行高效地插入和删除操作的有序序列。
LinkedList同ArrayList一样都为List的一个实现类,因此它的遍历方式和常用方法同List基本相同。
public static void main(String[] args) {
LinkedList ls=new LinkedList();
ls.add("哈哈");
ls.add("呵呵");
ls.add("嘿嘿");
System.out.println("getFirst:"+ls.getFirst());
System.out.println("getFirst:"+ls.peek());
System.out.println("getFirst:"+ls.peekFirst());
}
Set 接口中的元素无序不可重复,不包含重复元素,最多包含一个 null,元素没有顺序 。
Set继承了Collection接口的所有方法,没有新增方法。
Set同父级接口Collection相同,没有索引,只能通过增强for和迭代器来遍历元素。
HashSet 是 Set 接口的一个子类,主要的特点是:里面不能存放重复元素,而且采用散列的存储方法,所以没有顺序。这里所说的没有顺序是指:元素插入的顺序与输出的顺序不一致。
由于是无序,无法操作索引,因此与Set相同,只能通过增强for和迭代器遍历。
方法摘要 | |
---|---|
boolean |
add(E e) 如果此 set 中尚未包含指定元素,则添加指定元素。 |
void |
clear() 从此 set 中移除所有元素。 |
Object |
clone() 返回此 HashSet 实例的浅表副本:并没有复制这些元素本身。 |
boolean |
contains(Object o) 如果此 set 包含指定元素,则返回 true。 |
boolean |
isEmpty() 如果此 set 不包含任何元素,则返回 true。 |
Iterator |
iterator() 返回对此 set 中元素进行迭代的迭代器。 |
boolean |
remove(Object o) 如果指定元素存在于此 set 中,则将其移除。 |
int |
size() 返回此 set 中的元素的数量(set 的容量)。 |
注意:Set下还有个TreeSet类
- 它是有序的(排序--升序) 不可重复的
- 使用二叉树结构存储
public class HashSetDemo12 {
public static void main(String[] args) {
HashSet set=new HashSet<>();
set.add(new User("李健",40));
set.add(new User("谢霆锋",41));
set.add(new User("李健",40));
System.out.println(set.size());
for(User u:set){
System.out.println(u.hashCode());
}
TreeSet tr=new TreeSet();
tr.add("c");
tr.add("b");
tr.add("哈哈");
tr.add("a");
tr.add("呵呵");
tr.add("e");
System.out.println(tr);
System.out.println(tr.ceiling("b")); //c
System.out.println(tr.floor("b")); //a
System.out.println(tr.higher("b")); //c
System.out.println(tr.pollFirst()); //c
}
}
class User{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) 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;
}
}
Map是一种用来存储键值对形式的数据的接口:K(key) - V(value)。
Map 接口的实现类有 HashMap 和 TreeMap 等。
- HashMap:线程不安全,效率高,允许 key 或 value 为 null。
- HashTable:线程安全,效率低,不允许 key 或 value 为 null。
- Properties:Hashtable 的子类,key 和 value 都是 string。
方法摘要 | |
---|---|
void |
clear() 从此映射中移除所有映射关系(可选操作)。 |
boolean |
containsKey(Object key) 如果此映射包含指定键的映射关系,则返回 true。 |
boolean |
containsValue(Object value) 如果此映射将一个或多个键映射到指定值,则返回 true。 |
Set |
entrySet() 返回此映射中包含的映射关系的 Set 视图。 |
boolean |
equals(Object o) 比较指定的对象与此映射是否相等。 |
V |
get(Object key) 返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null 。 |
int |
hashCode() 返回此映射的哈希码值。 |
boolean |
isEmpty() 如果此映射未包含键-值映射关系,则返回 true。 |
Set |
keySet() 返回此映射中包含的键的 Set 视图。 |
V |
put(K key, V value) 将指定的值与此映射中的指定键关联(可选操作)。 |
void |
putAll(Map extends K,? extends V> m) 从指定映射中将所有映射关系复制到此映射中(可选操作)。 |
V |
remove(Object key) 如果存在一个键的映射关系,则将其从此映射中移除(可选操作)。 |
int |
size() 返回此映射中的键-值映射关系数。 |
Collection |
values() 返回此映射中包含的值的 Collection 视图。 |
Map因为是无序的,所以可以用到增强for循环和迭代器去遍历,又因为有键 - 值的存在,所以可以用键或者值去进行遍历。
Map中还提供了另外一种遍历方法,使用Map.entrySet(); 这个方法返回的是一个Set
public static void main(String[] args) {
Map map = new HashMap();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
//第一种:普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (String key : map.keySet()) {
System.out.println("key= "+ key + " and value= " + map.get(key));
}
//第二种
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第三种:推荐,尤其是容量大时
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry entry : map.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第四种
System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (String v : map.values()) {
System.out.println("value= " + v);
}
}
引用数据类型去重,我们把内容一样认为相同,解决办法:重写hashCode和equals方法。
public class MapTest05 {
public static void main(String[] args) {
Map maps=new HashMap<>();
maps.put(new Hero("亚瑟",450),1);
maps.put(new Hero("李白",18888),2);
maps.put(new Hero("李白",18888),3);
System.out.println(maps.size());
}
}
class Hero{
private String name;
private int money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public Hero(String name, int money) {
super();
this.name = name;
this.money = money;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + money;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Hero other = (Hero) obj;
if (money != other.money)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
Properties为Hashtable的子类,要求键与值只能为字符串,不能为null,常用于配置文件(与外界交互的信息) 即内存与存储介质(文件、数据库、网络、服务器内存等)交互。
public class PropertiesDemo07 {
/*pro.setProperty("name", "裴秀智");
pro.setProperty("age", "25");
System.out.println(pro.getProperty("name"));
*/
public static void main(String[] args) throws IOException {
Properties pro = new Properties();
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
System.out.println(pro.getProperty("name"));
}
}
类 java.util.Collections 提供了对容器操作的工具方法,与 Arrays 使用差不多。
- void sort(List) //对 List 容器内的元素排序,按照升序进行排序。
- void shuffle(List) //对 List 容器内的元素进行随机排列
- void reverse(List) //对 List 容器内的元素进行逆续排列
- void fill(List, Object) //用一个特定的对象重写整个 List 容器
- int binarySearch(List, Object)//采用折半查找的方法查找特定对象
public class CollectionsDemo06 {
public static void main(String[] args) {
List ls=new ArrayList();
ls.add("杨千嬅");
ls.add("杨超越");
ls.add("裴秀智");
ls.add("全智贤");
System.out.println(ls);
Collections.reverse(ls);
System.out.println(ls);
Collections.shuffle(ls);
System.out.println(ls);
// Collections.fill(ls, "范冰冰");
System.out.println(ls);
Collections.sort(ls);
System.out.println(ls);
System.out.println(Collections.binarySearch(ls, "裴秀智"));
GirlFriend g1=new GirlFriend("nice",0);
GirlFriend g2=new GirlFriend("soso",1);
GirlFriend g3=new GirlFriend("good",0);
List list=new ArrayList();
list.add(g1);
list.add(g2);
list.add(g3);
System.out.println(list);
Collections.sort(list);
System.out.println(list);
}
}
class GirlFriend implements Comparable{
private String body;
private int sex; //1 男孩 0女孩
public GirlFriend(String body, int sex) {
super();
this.body = body;
this.sex = sex;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
@Override
public String toString() {
return "GirlFriend [body=" + body + ", sex=" + sex + "]";
}
@Override
public int compareTo(GirlFriend o) {
return this.sex-o.sex;
}
}
在 Java 中, File类是一种用来表示文件和目录路径名的抽象形式。
public boolean createNewFile()
public boolean mkdirs()
public boolean mkdir()
public class Create {
/**
* @author Xss
*/
public static void main(String[] args) throws IOException {
File file = new File("xy.txt");//相对路径,refresh当前项目或f5
System.out.println(file.createNewFile());//如果没有就创建,返回true
File file2 = new File("xy");
System.out.println(file2.createNewFile());
File dir1 = new File("aaa");
System.out.println(dir1.mkdir());
File dir2 = new File("bbb.txt");//这样写是可以的,文件夹也是可以有后缀的
System.out.println(dir2.mkdir());
File dir3 = new File("ccc\\ddd");
System.out.println(dir3.mkdirs()); //创建多级目录
File dir4 = new File("bbb.txt\\ddd");
System.out.println(dir4.mkdirs()); //创建多级目录
}
}
public boolean renameTo(File dest)
public boolean delete()
重命名注意事项
* 如果路径名相同,就是改名。
* 如果路径名不同,就是改名并剪切。
删除注意事项:
* Java中的删除不走回收站。
* 要删除一个文件夹,请注意该文件夹内不能包含文件或者文件夹。
public class Name {
/**
* @author Xss
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file1 = new File("xy.txt");
File file2 = new File("D:\\xxx.txt");
//改名并剪贴
System.out.println(file1.renameTo(file2));
System.out.println(file2.delete());
}
}
public boolean isDirectory():判断是否是目录
public boolean isFile():判断是否是文件
public boolean exists():判断是否存在
public boolean canRead():判断是否可读
public boolean canWrite():判断是否可写
public boolean isHidden():判断是否隐藏
public class judge {
/**
*
* @author Xss
*
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File dir0 = new File("ccc");
System.out.println(dir0.isDirectory()); //判断是否是文件夹
File dir1 = new File("zzz");
System.out.println(dir1.isDirectory());
System.out.println(dir0.isFile());//判断是否是文件
System.out.println(dir1.isFile());
File file = new File("zzz");
file.setReadable(false);
System.out.println(file.canRead()); //windows系统认为所有的文件都是可读的
file.setWritable(true);
System.out.println(file.canWrite()); //windows系统可以设置为不可写
File file2 = new File("aaa.txt");
System.out.println(file2.isHidden());//判断是否是隐藏文件
System.out.println(file.isHidden());
}
}
public String getAbsolutePath()
public String getPath()
public String getName()
public long length()
public long lastModified()
public String[ ] list()
public String[] list(FilenameFilter filter)
public class d {
/**
* @author Xss
*
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file1 = new File("ccc.txt");
System.out.println(file1.createNewFile());
File file2 = new File("f:\\ccc.txt");
System.out.println(file2.createNewFile());
System.out.println(file1.getAbsolutePath());//获取绝对路径
//D:\Android\adt-bundle-windows-x86\workplace\dsdsdad\ccc.txt
System.out.println(file2.getAbsolutePath());
//D:\ccc.txt
System.out.println(file1.getPath());//获取构造方法中传入路径,ccc.txt
System.out.println(file2.getPath());//D:\ccc.txt
System.out.println(file1.getName());//获取文件或者文件的名字,ccc.txt
System.out.println(file2.getName());//ccc.txt
System.out.println(file1.length());//0,e-1
Date d = new Date(file1.lastModified());//文件的最后修改时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
System.out.println(sdf.format(d));//ok
File dir = new File("F:\\tools\\java\\util");
String[] arr = dir.list(); //仅为了获取文件名
for (String string : arr) {
System.out.println(string);
}
File[] subFiles = dir.listFiles();
for (File file : subFiles) { //获取文件对象
System.out.println(file);
}
}
}