当前位置:网站首页>Common methods of file class
Common methods of file class
2022-07-03 07:21:00 【four thousand three hundred and forty-three】

package File;
import org.junit.Test;
import java.io.File;
import java.util.Date;
public class FileMethod {
@Test
public void test1(){
File file1 = new File("hello.txt");
File file2 = new File("D:\\java project\\day07\\he.txt");
System.out.println(file1.getAbsoluteFile());
System.out.println(file1.getPath());
System.out.println(file1.getName());
System.out.println(file1.getParent());
System.out.println(file1.length());
System.out.println(new Date(file1.lastModified()));
System.out.println("************");
System.out.println(file2.getAbsoluteFile());
System.out.println(file2.getPath());
System.out.println(file2.getName());
System.out.println(file2.getParent());
System.out.println(file2.length());
System.out.println(file2.lastModified());
}
@Test
public void test2(){
// The following two methods apply to file directories :
// public String[] list(): Gets the name array of all files or file directories in the specified directory ( In the way of relative path )
// public File[] listFiles(): Get all files or file directories in the specified directory File Array ( In the form of absolute path )
File file = new File("D:\\java project");
String[] list = file.list();
for(Object obj : list){
System.out.println(obj);
}
File[] listFiles = file.listFiles();
for(Object obj:listFiles){
System.out.println(obj);
}
}
@Test
public void test3(){
// public boolean renameTo(File dest) Rename the file to the specified file path
// With file1.renameTo(file2) For example
// To ensure return true, need file1 There is ,file2 Does not exist in the hard disk
File file1 = new File("hello.txt");
File file2 = new File("D:\\java project\\day07\\src\\File\\hi.txt");
boolean renameTo = file1.renameTo(file2);
System.out.println(renameTo);
}
}



Files already in the hard disk , stay new file Object time , The corresponding values in memory will be assigned
No documents , When new file Object time , The corresponding values in memory are all default values .
package File;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
public class FileMethod2 {
@Test
public void test1(){
File file1 = new File("D:\\java project\\day07");
System.out.println(file1.isDirectory());
System.out.println(file1.isFile());
System.out.println(file1.exists());
}
@Test
public void test2() throws IOException {
File file1 = new File("hello.txt");
// File creation
if(!file1.exists()){
file1.createNewFile(); // It is equivalent to creating a new
System.out.println(" Create success ");
}else {
file1.delete();
System.out.println(" Delete successful ");
}
}
@Test
public void test3(){
// File directory creation
File file1 = new File("F:\\test\\io2\\io1");
boolean mkdir = file1.mkdir();
if(mkdir){
System.out.println(" create a file 1 Object success ");
}
File file2 = new File("F:\\test\\io2\\io3");
boolean mkdirs = file2.mkdirs();
if(mkdirs){
System.out.println(" create a file 2 Object success ");
}
}
}
边栏推荐
- Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
- Final, override, polymorphism, abstraction, interface
- dataworks自定义函数开发环境搭建
- POI excel percentage
- 10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
- Summary of abnormal mechanism of interview
- Interfaces and related concepts
- Resthighlevelclient gets the mapping of an index
- Setting up the development environment of dataworks custom function
- Advanced API (local simulation download file)
猜你喜欢

JMeter test result output

PAT甲级真题1166

Recursion, Fibonacci sequence

Summary of abnormal mechanism of interview

高并发内存池

How to specify the execution order for multiple global exception handling classes

带你全流程,全方位的了解属于测试的软件事故
![[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones](/img/d0/850e095a43610366d6144b2471f3f7.jpg)
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones

专题 | 同步 异步
![[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*](/img/1f/f579110a408c5b5a094733be57ed90.jpg)
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
随机推荐
Talk about floating
深度学习参数初始化(一)Xavier初始化 含代码
Advanced API (serialization & deserialization)
Arduino 软串口通信 的几点体会
带你全流程,全方位的了解属于测试的软件事故
La différence entre le let Typescript et le Var
Crontab scheduled task
Some experiences of Arduino soft serial port communication
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
Hash table, generic
Homology policy / cross domain and cross domain solutions /web security attacks CSRF and XSS
Distributed lock
Basic components and intermediate components
crontab定时任务
I. D3.js hello world
PHP install the spool extension
SecureCRT取消Session记录的密码
Win 2008 R2 crashed at the final installation stage
IP home online query platform
Take you through the whole process and comprehensively understand the software accidents that belong to testing