Summary of common methods of Java File class_javascript skills
The Java File class is very powerful. You can basically perform all operations on files using Java. This article will conduct a detailed analysis of the Java File file operation class and briefly introduce the common methods in the File class. Java developers in need can take a look.
Constructor
public class FileDemo {
Public static void main(String[] args){
//Constructor File(String pathname)
File f1 =new File("c:\abc\1.txt");
//File(String parent,String child)
File f2 =new File("c:\abc","2.txt");
//File(File parent,String child)
File f3 =new File("c:" File.separator "abc");//separator cross-platform separator
File f4 =new File(f3,"3.txt");
System.out.println(f1);//c:abc1.txt
}
}
Creation method
1.boolean createNewFile() returns true if it does not exist and false if it exists
2.boolean mkdir() creates directory
3.boolean mkdirs() creates multi-level directories
Deletion method
1.boolean delete()
2.boolean deleteOnExit() Delete the file after completion
import java.io.File;
import java.io.IOException;
public class FileDemo2 {
Public static void main(String[] args){
File f =new File("d:\1.txt");
try {
System.out.println(f.createNewFile());//Returns false when the file exists
System.out.println(f.delete());//Return false
when the file does not exist } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Judgment method
1.boolean canExecute() determines whether the file is executable
2.boolean canRead() determines whether the file is readable
3.boolean canWrite() determines whether the file can be written
4.boolean exists() determines whether the file exists
5.boolean isDirectory()
6.boolean isFile()
7.boolean isHidden()
8.boolean isAbsolute() determines whether it is an absolute path or the file does not exist
How to get it
1.String getName()
2.String getPath()
3.String getAbsolutePath()
4.String getParent()//If there is no parent directory, return null
5.long lastModified()//Get the time of the last modification
6.long length()
7.boolean renameTo(File f)
8.File[] liseRoots()//Get the machine drive letter
9.String[] list()
10.String[] list(FilenameFilter filter)
List files and folders under the disk
public class FileDemo3 {
public static void main(String[] args){
File[] files =File.listRoots();
for(File file:files){
System.out.println(file);
if(file.length()>0){
String[] filenames =file.list();
for(String filename:filenames){
System.out.println(filename);
}
}
}
}
}
文件过滤
import java.io.File;
import java.io.FilenameFilter;
public class FileDemo4 {
public static void main(String[] args){
File[] files =File.listRoots();
for(File file:files){
System.out.println(file);
if(file.length()>0){
String[] filenames =file.list(new FilenameFilter(){
//file 过滤目录 name 文件名
public boolean accept(File file,String filename){
return filename.endsWith(".mp3");
}
});
for(String filename:filenames){
System.out.println(filename);
}
}
}
}
}
File[] listFiles()
File[] listFiles(FilenameFilter filter)
利用递归列出全部文件
public class FileDemo5 {
public static void main(String[] args){
File f =new File("e:\音樂");
showDir(f);
}
public static void showDir(File dir){
System.out.println(dir);
File[] files =dir.listFiles();
for(File file:files){
if(file.isDirectory())
showDir(file);
else
System.out.println(file);
}
}
}
Move files
Find all the .java files in the d drive, copy them to the c:jad directory, and change the types of all files from .java to .jad.
public class Test5 {
Public static void main(String[] args){
File f1 = new File("d:\");
moveFile(f1);
}
public static void moveFile(File dir){
File[] files=dir.listFiles();
for(File file:files){
If(file.isDirectory())
moveFile(file);
else{
If(file.getName().endsWith(".java"))
file.renameTo(new File("c:\jad\"
file.getName().substring(0,file.getName().lastIndexOf('.')) ".jad"));
}
}
}
}
The above are all the properties and methods of the Java File class. We only need to simply call the above methods to complete the operation of the specified file. I hope this article will be helpful to you.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
