java file
Posted nedrain
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java file相关的知识,希望对你有一定的参考价值。
@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.");
}
}
}
以上是关于java file的主要内容,如果未能解决你的问题,请参考以下文章
php代码片段: sendFile/videoStream/sendEmail/phpexcel/ffmpeg/zip
[异常解决] Make nRF51 DFU Project Appear "fatal error: uECC.h: No such file or directory"(代码片段
Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 错误的解决(代码片段