Home Web Front-end JS Tutorial Summary of common methods of Java File class_javascript skills

Summary of common methods of Java File class_javascript skills

May 16, 2016 pm 04:08 PM
file class java Common methods

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

Copy code The code is as follows:

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

Copy code The code is as follows:

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

Copy code The code is as follows:

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.

Copy code The code is as follows:

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.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

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

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

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

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

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

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

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

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

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

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

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

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

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.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

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.

See all articles