The file class is an abstract representation of files and directory names in Java. The file class can be used to create, delete, rename, get paths, create time, and so on, and is the only operation related to the file itself . Therefore, it is necessary to study the file class well.
Main methods of Use:
1.public file (String pathname)----Construction of a document instance based on its path
2.public Boolean CreateNewFile () throws IOException-----Create file
3.public Boolean Delete ()----------Delete file
4.public String getParent ()----------get the top-level path to the file
5.public boolean isdirectory ()------Determine if the folder is not
6.public boolean isfile ()------Determine if the file is not
7.public string[]list ()------------lists the file names in the folder
8.public file[] listfile ()------------list all file instances of a folder
9.public boolean mkdir ()----------Create a folder
10.public boolean renameto (file dest)-----files renamed (can understand moving files)
11.public long Length ()-------Return file size
12.String getpath ()-----------path name string
Example code:
-
Package com;
-
-
Import Java.io.File;
-
Import java.io.IOException;
-
-
public class Test {
-
-
public static void Main (string[] args) {
-
-
File.separator is cross-platform, under Windows for D:\\1.txt,linux d:/1.txt
-
File File=new file ("D:" +file.separator+ "1.txt");
-
File does not exist
-
if (!file.exists ())
-
{
-
try {
-
if (File.createnewfile ())//Create File
-
System.out.println ("File creation succeeded");
-
} catch (IOException e) {
-
-
E.printstacktrace ();
-
}
-
}
-
Get the top-level path to the file
-
SYSTEM.OUT.PRINTLN ("Parent directory:" +file.getparent ());
-
Determine if it is a directory
-
System.out.println ("is directory:" +file.isdirectory ());
-
Determine if it is a file
-
System.out.println ("Whether it is a file:" +file.isfile ());
-
Get File size
-
SYSTEM.OUT.PRINTLN ("File Size:" +file.length ());
-
File path
-
System.out.println (File.getpath ());
-
-
Rename, when the return value is true. The equivalent of moving d:\\1.txt to l:\\kk22.php.
-
if (File.renameto (New file ("l:\\kk22.php")))
-
System.out.println ("renaming succeeded");
-
else {
-
System.out.println ("rename failed");
-
}
-
-
Point to renamed File
-
File=new File ("l:\\kk22.php");
-
if (File.delete ())
-
SYSTEM.OUT.PRINTLN ("File deletion succeeded");
-
-
--------------------------------------Catalog Demo
-
-
File=new File ("L:" +file.separator+ "javatest");
-
does not exist
-
if (!file.exists ())
-
{
-
File.mkdir ();//Create the Directory
-
}
-
Add files to this directory
-
String temp;
-
for (int i=0;i<4;i++)
-
{
-
Temp= "";
-
temp= "L:" +file.separator+ "javatest" +file.separator+i+ "TXT";
-
File Ok=new file (temp);
-
try {
-
Ok.createnewfile ();//Create the file
-
} catch (IOException e) {
-
TODO auto-generated Catch block
-
E.printstacktrace ();
-
}//
-
}
-
Get the file name under this directory
-
String[] Name=file.list ();
-
for (int i=0;i<4;i++)
-
System.out.println (Name[i]);
-
Get a file instance under this directory
-
File[] Files=file.listfiles ();
-
for (int i=0;i<4;i++)
-
System.out.println (Files[i].getpath ());
-
-
}
-
-
}
Copy Code
File can only get the files path creation time and other information, as well as the creation and deletion of files, the contents of the file can not be obtained and modified.
The Java IO Stream is required to obtain and modify the specific contents of the file. The IO-related operations are then explained.
The file class of Java