Java File Class Learning < two >

Source: Internet
Author: User
Tags dateformat

1. The file class provides a way to delete files or an empty folder, and provides a way to determine whether a file or folder exists, whether it is a file, a folder, whether it is a hidden file, and whether it is an absolute path.


Package com.fish.file;

Import Java.io.File;


/*

Delete:

Delete () deletes a file or an empty folder, if it is a folder and is not empty, it cannot be deleted, returns true successfully, and the failure returns false.

Deleteonexit () when the virtual machine terminates, requests that the file or directory represented by this abstract pathname be deleted, and that temporary files created when the program is abnormal can also be deleted



Judge:

Exists () whether the file or folder exists.

Isfile () is a file and is always false if it does not exist.

Isdirectory () is a directory and is always false if it does not exist.

Ishidden () Whether it is a hidden file or whether it is a hidden directory.

Isabsolute () tests whether this abstract path name is an absolute pathname.




*/

public class Demo4 {

public static void Main (string[] args) {

/*

deleted.

*/

File FILE0 = new file ("E:\\a.txt");

System.out.println ("Delete succeeded?") "+ file0.delete ()); The Delete method cannot be used to delete a non-empty folder. The Delete method deletes a file immediately.

File0.deleteonexit ();  Delete the file when the JVM exits. Typically used to delete temporary files.

System.out.println ("hahaha");

/*

Judge

*/

File File = new file (".. \\.. \\a.txt ");

System.out.println ("Does it exist?" "+ file.exists ());

System.out.println ("Judging whether it is a file:" +file.isfile ()); Returns False if the file returns True.

SYSTEM.OUT.PRINTLN ("Determine if it is a folder:" + file.isdirectory ()); is the folder that returns ture, otherwise false.

System.out.println ("is hidden file:" + File.ishidden ());

System.out.println ("Is it the absolute way?" "+ File.isabsolute ());

}


}


/*

Did the deletion succeed? True

Ha ha haha

Is it there? False

Determine if it is a file: false

Determine if it is a folder: false

Is the hidden file: false

Is it the absolute way? False

*/




2. The file class provides the name of the files or folders, relative paths, absolute paths, parent directories, file sizes, and the modified time

Package com.fish.file;


Import Java.io.File;

Import Java.sql.Date;

Import Java.text.SimpleDateFormat;


/*

Get:

GetName () Gets the name of the file or folder and does not contain the ancestor path.

GetPath () returns an absolute path, which can be a relative path, but the directory is specified

GetAbsolutePath () gets the absolute path to the file, and it doesn't matter if the file exists

Length () Gets the size of the file (in bytes) and returns 0L if the file does not exist, and returns 0L if it is a folder.

GetParent () returns the pathname string of the parent directory for this abstract pathname, or null if the pathname does not specify a parent directory.

LastModified () Gets the last time it was modified.


*/

public class demo5{


public static void Main (string[] args) {

File File = new file (".. \\.. \\a.txt ");

System.out.println ("File name:" + file.getname ());

System.out.println ("Get absolute path:" + File.getpath ());

System.out.println ("GetAbsolutePath Get Absolute path:" +file.getabsolutepath ());

System.out.println ("Gets the size of the file (in bytes):" + file.length ());

System.out.println ("Get the parent path of the file:" + file.getparent ());

Converting to a Date object using millisecond values

Long lastmodified = file.lastmodified ();

Date date = new Date (lastmodified);

SimpleDateFormat DateFormat = new SimpleDateFormat ("yyyy mm month DD Day HH:mm:ss");

System.out.println ("Gets the last modification time (millisecond value):" + dateformat.format (date));

}

}


/*

File name: a.txt

Get absolute path:.. \.. \a.txt

GetAbsolutePath get absolute path: E:\code\JavaSE\day19\day19\. \.. \a.txt

Gets the size of the file (in bytes): 0

Gets the parent path of the file:. \..

Gets the last modified time (in milliseconds): January 01, 1970 08:00:00

*/




3. The file class provides a way to list all files or directories in a directory


Package com.fish.file;

Import Java.io.File;


/*

Folder-Related:

Staic file[] Listroots () lists all the root directories (in window is the drive letter for all systems)

List () returns the file or directory name in the directory containing the hidden file. For files such operations will return NULL.

Listfiles () returns a file or directory object (instance of the file class) in the directory that contains the hidden file. For files such operations will return NULL.



The list (FilenameFilter filter ) returns a sub-file or subdirectory that specifies the filter criteria in the current directory. For files such operations will return NULL.

Listfiles (filenamefilter filter) Returns a sub-file or subdirectory that specifies the filter criteria in the current directory. For files such operations will return NULL.

*/

public class Demo6 {

public static void Main (string[] args) {

File[] roots = File.listroots (); List all the root directories

for (File file:roots) {

System.out.println (file);

}

File File = new file ("D:/greensoft");

string[] FileNames = File.list (); Stores all sub-filenames and subfolder names below the current folder in a string-type array. Not including grandchildren.

for (String filename:filenames) {

System.out.println (FileName);

}

file[] files = file.listfiles (); Use a File object description for all sub-files and subfolders below the current folder, and then store these file objects in a file array to return

for (File fileitem:files) {

System.out.println ("File name:" + fileitem.getname ());

}

}


}


/*

C +

D:\

E:\

apache-maven-3.3.3

Apache-tomcat-6.0.44-windows-x64

Eclipse-jee-luna-r-win32-x86_64

Eyeguard

Jd-gui-0.3.6.windows

File name: apache-maven-3.3.3

File name: apache-tomcat-6.0.44-windows-x64

File name: eclipse-jee-luna-r-win32-x86_64

File name: Eyeguard

File name: jd-gui-0.3.6.windows

*/



4, the use of filenamefilter filter

Package com.fish.file;



Import Java.io.File;

Import Java.io.FilenameFilter;

/*

Requirement 1: Specify a folder, and then all Java files under that folder.


Requirement 2: Specify a folder, and then list all sub-files and folders underneath the folder, but in the following format:

File:

File name 1

File Name 2

File name 3

..


Folder:

Folder name 1

Folder Name 2

Folder name 3

....


Listfiles (filenamefilter filter) Returns a sub-file or subdirectory that specifies the filter criteria in the current directory. For files such operations will return NULL. Returns a file array

The list (FilenameFilter filter ) returns a sub-file or subdirectory that specifies the filter criteria in the current directory. For files such operations will return NULL. The return value is an array of strings


*/


Customizing a file name filter

Class Myfilter implements filenamefilter{


@Override

Public Boolean Accept (File dir, String name) {

System.out.println ("folder:" +dir+ "FileName:" + name);

Return Name.endswith (". Java");

}

}




public class Demo7 {

public static void Main (string[] args) {

File dir = new file ("F:\\code");

ListJava2 (dir);

}

public static void ListJava2 (File dir) {

file[] files = dir.listfiles (new Myfilter ()); Get all the sub-files and folders underneath the folder.

for (File file:files) {

System.out.println (File.getname ());

}

}

List all Java files

public static void Listjava (File dir) {

file[] files = dir.listfiles (); Get all the Sub files

for (File file:files) {

String fileName = File.getname ();

/*if (Filename.endswith (". Java") &&file.isfile ()) {

System.out.println (FileName);

}*/

if (Filename.matches (". +\\.java") &&file.isfile ()) {

System.out.println (FileName);

}

}

}

public static void ListFile (File dir) {

file[] files = dir.listfiles ();//Get to all sub-files

System.out.println ("File:");

for (File fileitem:files) {

if (Fileitem.isfile ()) {

System.out.println ("\ T" +fileitem.getname ());

}

}

System.out.println ("File false:");

for (File fileitem:files) {

if (Fileitem.isdirectory ()) {

System.out.println ("\ T" +fileitem.getname ());

}

}

}

}


This article from "Small Fish Blog" blog, declined reprint!

Java File Class Learning < two >

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.