2020.03.02 File类

package com.guoyun.view;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
* ClassName:
* Function: ADD FUNCTION
* Reason: ADD REASON
*
* @author
* @Date
* @since Ver 1.1
*/
public class Test1 {


public static void main(String[] args) {
//renameTo方法
// File file3=new File("E:/JAVA/Test");
// File file4=new File("E:/JAVA/Test2");
// System.out.println(file3.renameTo(file4));
//带盘符的为绝对路径
File file=new File("E:/JAVA/新建文件夹/Test");
if(!(file.exists())){
file.mkdirs();
}
File file1=new File(file,"aojie.txt");
if(!(file1.exists())){
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
File file2=new File(file,"aoji.txt");
if(!(file2.exists())){
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
File file3=new File(file,"aojie.jpg");
if(!(file3.exists())){
try {
file3.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

// if (file2.exists()) {
//
// file2.delete();
// System.out.println("已找到该文件并删除成功!");
// }
String[] temp=file.list();
for (int i = 0; i <temp.length ; i++) {
if(temp[i].endsWith(".jpg")){
System.out.println(temp[i]);
}
}

System.out.println(printLength(file));
File file4=new File("hello.txt");
try {
file4.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(file4.exists());

}
public static int printLength(File file){
File[] files=file.listFiles();
int count=0;
for (int i = 0; i <files.length ; i++) {
if(files[i].isDirectory()){
printLength(files[i]);
}else {
count+=files[i].length();
}

}
return count;
}
}

猜你喜欢

转载自www.cnblogs.com/aojie/p/12397862.html