Original link Author: Jakob Jenkov Translator: Li Jing ([email protected])
The file class in the Java IO API allows you to access the underlying file system through the file class, which you can do the following:
- Detects if a file exists
- Read file length
- Rename or move a file
- deleting files
- Detect if a path is a file or a directory
- Read a list of files in a directory
Note: file can only access files and file system metadata. If you want to read and write file content, you need to use FileInputStream, FileOutputStream, or Randomaccessfile. If you are using Java NIO and want to use the full NIO solution, you will use the Java.nio.FileChannel (or you can use file).
instantiate a Java.io.File object
Before using file, you must have a file object, which is an instantiated code example:
1 |
File file = newFile("c:\\data\\input-file.txt"); |
It's simple, right? The file class also has constructors for many different instantiation methods.
detects if a file exists
Once you have a file object, you can detect whether the corresponding file exists. When the file does not exist, the constructor does not fail to execute. You're ready to create a file, right?
By calling the exists () method, you can detect whether a file exists and the code is as follows:
1 |
File file = newFile("c:\\data\\input-file.txt"); |
3 |
booleanfileExists = file.exists(); |
file Length
The length of the file can be obtained by calling length (), with the following code:
1 |
File file = newFile("c:\\data\\input-file.txt"); |
3 |
longlength = file.length(); |
Rename or move a file
You can rename (or move) the file by calling the Renameto () method in the file class, with the following code:
1 |
File file = newFile("c:\\data\\input-file.txt"); |
3 |
booleansuccess = file.renameTo(newFile("c:\\data\\new-file.txt")); |
Deleting Files
You can delete a file by calling the Delete () method, with the following code:
1 |
File file = newFile("c:\\data\\input-file.txt"); |
3 |
booleansuccess = file.delete(); |
The delete () method, like the rename () method, returns a Boolean value indicating whether the file was successfully deleted, as well as the reason for the same operation failure.
detect if a path is a file or a directory
The file object can point to either a file or a directory. By calling the Isdirectory () method, you can determine whether the current file object is pointing to files or directories. When the method return value is true, file points to the directory, otherwise to the file, the code is as follows:
1 |
File file = newFile("c:\\data"); |
3 |
booleanisDirectory = file.isDirectory(); |
read a list of files in a directory
You can get a list of all the files in a directory by calling the list () or the Listfiles () method. The list () method returns the string name of all files and subdirectories in the directory to which the current file object points (Translator Note: The file and its subdirectory names under subdirectories are not returned). The Listfiles () method returns the file object to which all files in the directory pointed to by the current file object are associated with subdirectories (the translator notes: Like the list () method, files and subdirectories under subdirectories are not returned). The code is as follows:
1 |
File file = newFile("c:\\data"); |
3 |
String[] fileNames = file.list(); |
5 |
File[] files = file.listFiles(); |
original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Java io:file
Java Io:file