File的常见方法
1,获取
1.1获取文件名称
1.2获取文件路径
1.3获取文件大小
1.4获取文件修改时间
public static void main(String[] args) throws IOException {
File file = new File("c:\\Users\\悠悠华\\Desktop\\test\\suibian.txt");
//文件名
String name = file.getName();
//文件绝对路径
String absPath = file.getAbsolutePath();
//文件相对路径
String path = file.getPath();
//文件大小
long len = file.length();
//文件上一次修改的时间戳
long time = file.lastModified();
System.out.println("name:"+name);
System.out.println("absPath:"+absPath);
System.out.println("path:"+path);
System.out.println("len:"+len);
System.out.println("time:"+time);
}
结果:
name:suibian.txt
absPath:c:\Users\悠悠华\Desktop\test\suibian.txt
path:c:\Users\悠悠华\Desktop\test\suibian.txt
len:36
time:1533195989748
2.创建与删除
public static void main(String[] args) throws IOException {
File file = new File("c:\\Users\\悠悠华\\Desktop\\test\\tttt.htm");
//1.创建文件
boolean b = file.createNewFile();
System.out.println(b);
boolean bb = file.delete();
//2.删除文件
System.out.println(bb);
File dir = new File("c:\\Users\\悠悠华\\Desktop\\test\\dir");
//3.创建文件夹
boolean bbb = dir.mkdir();
System.out.println(bbb);
//4.删除文件夹
boolean c = dir.delete();
System.out.println(c);
File dirs = new File("c:\\Users\\悠悠华\\Desktop\\test\\a\\b\\c");
//5.创建多级文件夹
boolean cc = dirs.mkdirs();
System.out.println(cc);
}
3.判断
public static void main(String[] args) throws IOException {
File file = new File("c:\\Users\\悠悠华\\Desktop\\test\\java");
//判断文件或文件夹是否存在
boolean b = file.exists();
System.out.println(b);
System.out.println("是文件夹吗?"+file.isDirectory());
System.out.println("是文件吗?"+file.isFile());
}
4.重命名renameTo(File)
public static void main(String[] args) throws IOException {
File file1 = new File("c:\\Users\\悠悠华\\Desktop\\test\\sb.txt");
File file2 = new File("c:\\Users\\悠悠华\\Desktop\\test\\gggg\\suibian.txt");
//若是在同一文件夹下则是重命名,若不是同一文件夹下就是剪切
boolean b = file1.renameTo(file2);
System.out.println(b);
}
5.获取盘符及空间大小
public static void main(String[] args) throws IOException {
//列出可用的文件系统根
File[] files = File.listRoots();
for(File file:files){
System.out.println(file);
}
File file2 = new File("c:\\");
System.out.println("空闲空间:"+file2.getFreeSpace());
System.out.println("空闲空间:"+file2.getUsableSpace());
System.out.println("总共的空间:"+file2.getTotalSpace());
}
结果:
C:\
D:\
E:\
F:\
G:\
H:\
空闲空间:51503419392
空闲空间:51503419392
总共的空间:125721554944
6.获取文件夹目录文件
public static void main(String[] args) throws IOException {
File file = new File("c:\\Users\\悠悠华\\Desktop\\test\\图片");
//获取当前文件夹目录下的文件 包含隐藏文件
String[] names = file.list();
for(String name:names){
System.out.println(name);
}
}
结果:
1.jpg
2.jpg
53b2569e093b1.jpg
6cb62df5e0fe9925727a36d938a85edf8cb17160.jpg
72fb35694fd4003f0c22878bb1aa4d75.gif
background.jpg
board.jpg
c罗.jpg
dc8c632762d0f7033b93829104fa513d2797c5fd.jpg
Digital_ColorMeter_64px_1186229_easyicon.net.png
IMG_9957.JPG
o4YBAFmf4xyADcikAAR_bBR3jdE597.jpg
timg (1).jpg
timg (2).jpg
timg.jpg
tom.jpg
u=1494540428,199285174&fm=27&gp=0.jpg
u=2740837157,3215561989&fm=173&app=25&f=JPEG.jpg
white.jpg
二哈.jpg
7.文件过滤器
public class Demo {
public static void main(String[] args) throws IOException {
//查找此文件目录下的所有的txt文件
File file = new File("c:\\Users\\悠悠华\\Desktop\\test");
String[] names = file.list(new FilterByName());
for(String name:names){
System.out.println(name);
}
}
}
class FilterByName implements FilenameFilter{
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".txt");
}
}
8.递归遍历文件夹
public static void listDeep(File dir,int level) {
//深度遍历文件夹
//首先现将目录打印出来
System.out.println(space(level)+dir.getName());
//缩进次数自增
level++;
File[] files = dir.listFiles();
for(int x = 0; x < files.length; x++){
//如果这个File对象是文件 就缩进并打印文件
if(files[x].isFile())
System.out.println(space(level)+files[x].getName());
//反之,如果是文件夹对象就递归调用自身
else{
listDeep(files[x],level);
}
}
}
private static String space(int level) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder();
for(int x = 0;x<level;x++)
sb.append(" ");
return sb.toString();
}
结果:
java
Arr.java
in
Arr.class
mydoc
allclasses-frame.html
allclasses-noframe.html
Arr.html
constant-values.html
deprecated-list.html
help-doc.html
index-all.html
index.html
overview-tree.html
package-frame.html
package-list
package-summary.html
package-tree.html
script.js
stylesheet.css
out
mydoc
allclasses-frame.html
allclasses-noframe.html
constant-values.html
deprecated-list.html
help-doc.html
index-all.html
index.html
overview-tree.html
package-frame.html
package-list
package-summary.html
package-tree.html
script.js
stylesheet.css
Test.html
Test.java
socket
TCPClient.class
TCPClient.java
TCPServer.class
TCPServer.java