package cd_one.code18.exer;
import org.junit.Test;
import java.io.File;
public class FileTest {
@Test
public void test(){
new File("d:\\io\\hello.txt");
//相对于某一个文件目录来讲的相对位置
File file2 = new File("ab");
System.out.println(file2.getAbsolutePath());
}
// public static void main(String[] args) {
// File file2 = new File("abc");
// System.out.println(file2.getAbsolutePath());
// }
@Test
public void test2(){
/**
* 参数1:一定是一个文件目录
* 参数2:可以是一个文件,也可以是一个文件目录
*/
File file1 = new File("d:\\io","abc.txt");//有斜杠绝对路径
File file2 = new File("abc","a12");//无斜杠,相对路径
File file3 = new File(file2,"ab.txt");
}
}
package cd_one.code18.exer;
import org.junit.Test;
import java.io.File;
public class FileTest1 {
@Test
public void test(){
File file1 = new File("hello.txt");
System.out.println(file1.getName());//hello.txt
System.out.println(file1.getPath());//hello.txt
System.out.println(file1.getAbsolutePath());//D:\javacode\com.xieqiwen.xqw\hello.txt
System.out.println(file1.getAbsoluteFile());//D:\javacode\com.xieqiwen.xqw\hello.txt
System.out.println(file1.getParent());//null,因为我写的是相对路径
System.out.println(file1.getAbsoluteFile().getParent());//这样就可以从上面转变成拿到文件目录
//D:\javacode\com.xieqiwen.xqw
System.out.println(file1.length());//0//6
System.out.println(file1.lastModified());//0//1710666653336
}
@Test
public void test2(){
File file1 = new File("D:\\io\\io1");
System.out.println(file1.getName());//io1
System.out.println(file1.getPath());//D:\io\io1
System.out.println(file1.getAbsolutePath());//D:\io\io1
System.out.println(file1.getAbsoluteFile());//D:\io\io1
System.out.println(file1.getParent());//D:\io
System.out.println(file1.getAbsoluteFile().getParent());//D:\io
System.out.println(file1.length());//0
System.out.println(file1.lastModified());//1710666788506
}
}
@Test
public void test4(){
File file1 = new File("hello.txt");
File file2 = new File("d:\\io\\abc.txt");//需要本来就不存在才能成功
boolean renameSuccess = file1.renameTo(file2);
System.out.println(renameSuccess?"重命名成功":"重命名失败");
}
@Test
public void test5(){
File file2 = new File("d:\\io\\abc.txt");
System.out.println(file2.exists());//true
System.out.println(file2.isDirectory());//false
System.out.println(file2.isFile());//true
System.out.println(file2.canRead());//true
System.out.println(file2.canWrite());//true
System.out.println(file2.isHidden());//false
System.out.println();
File file3 = new File("d:\\io");
System.out.println(file3.exists());//true
System.out.println(file3.isDirectory());//true
System.out.println(file3.isFile());//false
System.out.println(file3.canRead());//true
System.out.println(file3.canWrite());//true
System.out.println(file3.isHidden());//false
}
@Test
public void test6() throws IOException {
//文件的创建和删除
File file1 = new File("d:\\io\\hello.txt");
if(!file1.exists()){
boolean isSuccessed = file1.createNewFile();
if(isSuccessed){
System.out.println("创建成功");
}
}else{
System.out.println("此文件已存在");
System.out.println(file1.delete() ? "文件删除成功" : "文件删除失败");
}
}
@Test
public void test7(){
//前提 d:\\o文件目录存在。io2或io3目录不存在
File file1 = new File("d:\\io\\io3\\io5");
// file1.mkdir();
System.out.println(file1.delete());
//上层目录不在了
}
package cd_one.code18.exer;
import org.junit.Test;
import java.io.File;
public class Exer03 {
@Test
public void test(){
File file = new File("D:\\前端学习\\pink前端基础带资料");
printFileName(file);
}
public void printFileName(File file){
if(file.isFile()){
System.out.println(file.getName());
} else if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
printFileName(f);
}
}
}
}