java file
2021-04-19 21:29
标签:some jpg tac cto data tde efi sys getpath java file 标签:some jpg tac cto data tde efi sys getpath 原文地址:https://www.cnblogs.com/nedrain/p/13287804.html @Test
public void test1(){
File file1 = new File("/Users/truman/Desktop/Life");
System.out.println(file1);
System.out.println(file1.isDirectory());
File file2 = new File("/Users/truman", "Pictures");
System.out.println(file2);
System.out.println(file2.isDirectory());
File file3 = new File(file2, "aiaiai.jpg");
System.out.println(file3);
}
Common Methods
String getAbsolutePath()
String getPath()
String getName()
String getParent()
long length()
long lastModified()
@Test
public void testFileMethod(){
File file1 = new File("/Users/truman/Desktop");
System.out.println(file1.getAbsolutePath()); // /Users/truman/Desktop
System.out.println(file1.getPath()); // /Users/truman/Desktop
System.out.println(file1.getName()); // Desktop
System.out.println(file1.getParent()); // /Users/truman
System.out.println(file1.length()); // 672
System.out.println(file1.lastModified()); // 1593138078000
System.out.println(new Date(file1.lastModified())); // Fri Jun 26 10:21:18 CST 2020
}
String[] list()
String[] list = file1.list();
System.out.println(Arrays.toString(list));
// [Villanelle, Database, 数学建模书籍, .DS_Store, Something, .localized, ...]
FIle[] listFiles()
File[] files = file1.listFiles();
boolean renameTo(File file) (实际上是移动文件)
More Methods
boolean createNewFile()
@Test
public void testCreateFile(){
File file1 = new File("hi.txt");
if(!file1.exists()){
try {
file1.createNewFile();
System.out.println("New File Created.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
boolean delete()
@Test
public void testDelete(){
File file1 = new File("hi.txt");
if (file1.exists()){
file1.delete();
System.out.println("File Deleted.");
}
}
}
下一篇:c#中常见的算法(一)