In I/O processing, the most common is file operations, Java. the file processing classes in the IO package include file, fileinputstream, fileoutputstream, ramdomaccessfile, and filedescriptor. The interfaces include filenamefilter.
Class file provides a machine-independent method to describe the attributes of a file object. The following describes the methods provided in the file class.
◇ File or directory generation
Public file (string path);/* If the path actually exists, the file object
/* Indicates a directory. If path is a file name, the file object indicates a file. */
Public file (string path, string name); // path is the path name, and name is the file name
Public file (File Dir, string name); // DIR is the path name, and name is the file name
◇ File name Processing
String getname (); // get the name of a file (excluding the path)
String getpath (); // get the path name of a file.
String getabsolutepath (); // obtain the absolute path name of an object.
String getparent (); // obtain the name of the directory at the upper level of a file.
String renameto (File newname); // rename the current file name to
Full path
◇ File Property Test
Boolean exists (); // test whether the file indicated by the current file object exists
Boolean canwrite (); // test whether the current file can be written.
Boolean Canread (); // test whether the current file is readable
Boolean isfile (); // test whether the current file is a file (not a directory)
Boolean isdirectory (); // test whether the current file is a directory
◇ Common file information and tools
Long lastmodified (); // The last time the file was modified.
Long length (); // obtain the object length, in bytes.
Boolean Delete (); // Delete the current file
◇ Directory operations
Boolean mkdir (); // generate a path specified by this object based on the current object
String list (); // List objects in the current directory
Instance:
Java. Io
---------------------------------------------------------------
Package common;
Import java. Io .*;
Public class fileoperate {
Public fileoperate (){
}
/**
* Create a directory
* @ Param folderpath string such as C:/fqf
* @ Return Boolean
*/
Public void newfolder (string folderpath ){
Try {
String filepath = folderpath;
Filepath = filepath. tostring ();
Java. Io. File myfilepath = new java. Io. File (filepath );
If (! Myfilepath. exists ()){
Myfilepath. mkdir ();
}
}
Catch (exception e ){
System. Out. println ("Directory creation error ");
E. printstacktrace ();
}
}
/**
* Create a file
* @ Param filepathandname string file path and name such as C:/fqf.txt
* @ Param filecontent string File Content
* @ Return Boolean
*/
Public void newfile (string filepathandname, string filecontent ){
Try {
String filepath = filepathandname;
Filepath = filepath. tostring ();
File myfilepath = new file (filepath );
If (! Myfilepath. exists ()){
Myfilepath. createnewfile ();
}
Filewriter resultfile = new filewriter (myfilepath );
Printwriter myfile = new printwriter (resultfile );
String strcontent = filecontent;
Myfile. println (strcontent );
Resultfile. Close ();
}
Catch (exception e ){
System. Out. println ("Directory creation error ");
E. printstacktrace ();
}
}
/**
* Delete an object
* @ Param filepathandname string file path and name such as C:/fqf.txt
* @ Param filecontent string
* @ Return Boolean
*/
Public void delfile (string filepathandname ){
Try {
String filepath = filepathandname;
Filepath = filepath. tostring ();
Java. Io. File mydelfile = new java. Io. File (filepath );
Mydelfile. Delete ();
}
Catch (exception e ){
System. Out. println ("File Deletion error ");
E. printstacktrace ();
}
}
/**
* Delete A folder
* @ Param filepathandname string folder path and name such as C:/fqf
* @ Param filecontent string
* @ Return Boolean
*/
Public void delfolder (string folderpath ){
Try {
Delallfile (folderpath); // delete all contents
String filepath = folderpath;
Filepath = filepath. tostring ();
Java. Io. File myfilepath = new java. Io. File (filepath );
Myfilepath. Delete (); // delete an empty folder
}
Catch (exception e ){
System. Out. println ("Folder deletion error ");
E. printstacktrace ();
}
}
/**
* Delete all files in the folder
* @ Param path string folder path such as C:/fqf
*/
Public void delallfile (string path ){
File file = new file (PATH );
If (! File. exists ()){
Return;
}
If (! File. isdirectory ()){
Return;
}
String [] templist = file. List ();
File temp = NULL;
For (INT I = 0; I <templist. length; I ++ ){
If (path. endswith (file. separator )){
Temp = new file (path + templist [I]);
}
Else {
Temp = new file (path + file. Separator + templist [I]);
}
If (temp. isfile ()){
Temp. Delete ();
}
If (temp. isdirectory ()){
Delallfile (path + "/" + templist [I]); // delete the files in the folder first
Delfolder (path + "/" + templist [I]); // Delete the empty folder.
}
}
}
/**
* Copy a single file
* @ Param oldpath string original file path, for example, C:/fqf.txt
* @ Param newpath: the copied string path is F:/fqf.txt.
* @ Return Boolean
*/
Public void copyfile (string oldpath, string newpath ){
Try {
Int bytesum = 0;
Int byteread = 0;
File oldfile = new file (oldpath );
If (oldfile. exists () {// when the file exists
Inputstream instream = new fileinputstream (oldpath); // read the original file
Fileoutputstream FS = new fileoutputstream (newpath );
Byte [] buffer = new byte [1444];
Int length;
While (byteread = instream. Read (buffer ))! =-1 ){
Bytesum + = byteread; // file size in bytes
System. Out. println (bytesum );
FS. Write (buffer, 0, byteread );
}
Instream. Close ();
}
}
Catch (exception e ){
System. Out. println ("An error occurred while copying a single file ");
E. printstacktrace ();
}
}
/**
* Copy the entire folder.
* @ Param oldpath string original file path, for example, C:/fqf
* @ Param newpath: path after string copying, for example, F:/fqf/ff.
* @ Return Boolean
*/
Public void copyfolder (string oldpath, string newpath ){
Try {
(New file (newpath). mkdirs (); // create a new folder if the folder does not exist
File a = new file (oldpath );
String [] file = A. List ();
File temp = NULL;
For (INT I = 0; I <file. length; I ++ ){
If (oldpath. endswith (file. separator )){
Temp = new file (oldpath + file [I]);
}
Else {
Temp = new file (oldpath + file. Separator + file [I]);
}
If (temp. isfile ()){
Fileinputstream input = new fileinputstream (temp );
Fileoutputstream output = new fileoutputstream (newpath + "/" +
(Temp. getname (). tostring ());
Byte [] B = new byte [1, 1024*5];
Int Len;
While (LEN = input. Read (B ))! =-1 ){
Output. Write (B, 0, Len );
}
Output. Flush ();
Output. Close ();
Input. Close ();
}
If (temp. isdirectory () {// if it is a subfolder
Copyfolder (oldpath + "/" + file [I], newpath + "/" + file [I]);
}
}
}
Catch (exception e ){
System. Out. println ("An error occurred while copying the entire folder ");
E. printstacktrace ();
}
}
/**
* Move a file to a specified directory
* @ Param oldpath string such as: C:/fqf.txt
* @ Param newpath string such as: D:/fqf.txt
*/
Public void movefile (string oldpath, string newpath ){
Copyfile (oldpath, newpath );
Delfile (oldpath );
}
/**
* Move a file to a specified directory
* @ Param oldpath string such as: C:/fqf.txt
* @ Param newpath string such as: D:/fqf.txt
*/
Public void movefolder (string oldpath, string newpath ){
Copyfolder (oldpath, newpath );
Delfolder (oldpath );
}
}