File Class Summary:
Abstract representation of the path name of a file and directory (folder), which is simply a pathname, exists and does not exist, see the next operation. After the construction, it is a file name.
In addition to the folder on the hard disk, there is only files.
Each name after the first name in the abstract pathname represents a directory, and the last name can represent either a directory or a file. Empty abstract path names do not have a prefix and a name sequence.
Construction Method:
File (String pathname): Gets the file object based on a path
File (string parent, String child): Gets the file object based on a directory and a sub-file/directory
File (file parent, String child): Gets the file object based on a parent file object and a sub-file/directory
Create Features:
public boolean createnewfile (): Create file If there is such a file, it is not created, return false
public boolean mkdir (): Create folder If there is such a folder, it is not created, return false
public boolean mkdirs (): Create folder, if the parent folder does not exist, will help you to create it, if there is such a folder, return False
Note: Whether you want to create a file or a folder, you know best, do not tune the wrong way.
Delete function: public boolean delete ()
Attention:
A: If you create a file or folder where you forgot to write the drive path, the default is under Project path.
Delete in B:java does not go to recycle Bin.
C: To delete a folder, please note that the folder cannot contain files or folders
Renaming feature:p ublic boolean Renameto (File dest)
if the path name is the same, it is renamed.
if the path name is different, it is renamed and clipped.
path begins with a drive letter: absolute path C:\\a.txt
path does not start with a letter: relative path a.txt
Get Features:
Public String GetAbsolutePath (): Gets the absolute path
Public String GetPath (): Get relative path
Public String getName (): Get Name
public long Length (): Gets the length. Number of bytes
Public long LastModified (): Gets the last modification time, millisecond value, which can be calculated by the millisecond value time
Get Features:
Public string[] List (): Gets an array of names for all files or folders under the specified directory
Public file[] Listfiles (): Gets the file array of all files or folders under the specified directory
Determine if there is a file with a. jpg suffix in the e-drive directory, and if so, output this file name
Analysis:
A: Package E to determine the directory
B: Get file array for all files or folders in this directory
C: Iterate over the file array, get each file object, and then judge
D: Whether it is a file
Yes: continue to determine whether to end with. jpg
Yes: The file name is output
No: Ignore it.
No: Ignore it.
public class Filedemo {
public static void Main (string[] args) {
Package E to determine the directory
File File = new file ("e:\\");
Get file array for all files or folders in this directory
file[] Filearray = File.listfiles ();
Iterate over the file array, get each file object, and then judge
for (File F:filearray) {
Whether it is a file
if (F.isfile ()) {
Continue to determine whether to end with a. jpg
if (F.getname (). EndsWith (". jpg")) {
The file name is output
System.out.println (F.getname ());
}
}
}
}
}
Determine if there is a file with a. jpg suffix in the e-drive directory, and if so, output this file name
A: First obtain all, and then traverse the time, in turn, to determine if the condition is satisfied with the output.
B: When it is acquired, it is already satisfied, then the output can be.
To achieve this effect, you must learn an interface:File name Filter
Public string[] List (filenamefilter filter)
Public file[] Listfiles (filenamefilter filter)
public class FileDemo2 {
public static void Main (string[] args) {
Package E to determine the directory
File File = new file ("e:\\");
Get a string array of all files or folders in this directory
Public string[] List (filenamefilter filter)
string[] Strarray = file.list (new FilenameFilter () {
@Override
Public Boolean Accept (File dir, String name) {//tests whether the specified file should be included in a list of files and, if False, is not added to the list, or vice versa. You can see from the source that the method is traversed by the called
Through the source view of the file.list, it is found that this accept method is actually the way to execute each time in the traversal, as long as this method is false, will not be added to the file array. Override this method to determine if the requirement is met.
return new File (dir, name). Isfile () && name.endswith (". jpg");
}
});
Traverse
for (String S:strarray) {
System.out.println (s);
}
}
}
Requirements: E:\ storytelling \ Kingdoms The following video name revision to
00?_ Introduction. avi
Modified before://e:\ storytelling \ Kingdoms \ Kingdoms _001_[Storytelling Network-today is very happy, tomorrow on Io]_ Taoyuan three jieyi. avi
Modified://E:\ storytelling \ Kingdoms \001_ Taoyuan three jieyi. avi
Ideas:
A: Package Directory
B: Gets the file array for all files in this directory
C: Traverse the file array to get each file object
D: Stitch A new name, then rename it.
public class Filedemo {
public static void Main (string[] args) {
Package Catalog
File Srcfolder = new file ("E:\\ Storytelling \ Kingdoms");
Get file array for all files in this directory
file[] Filearray = Srcfolder.listfiles ();
Iterate over the file array to get each file object
for (File File:filearray) {
String name = File.getname (); Kingdoms _001_[Storytelling Network-today is very happy, tomorrow on Io]_ Taoyuan three jieyi. avi
int index = Name.indexof ("_");
String numberstring = name.substring (index + 1, index + 4);
int endIndex = Name.lastindexof ('_');
String namestring = name.substring (EndIndex);
String newName = Numberstring.concat (namestring); 001_ Taoyuan three jieyi. avi
File NewFile = new file (Srcfolder, newName); e:\\ storytelling \ Kingdoms \\001_ Taoyuan three jieyi. avi
Rename to
File.renameto (NewFile);
}
}
}
File Summary _day19