Java File Operations

Source: Internet
Author: User

1. Create a folder
Java code
File myFolderPath = new File (% 1 );
Try {
If (! MyFolderPath. exists ()){
MyFolderPath. mkdir ();
}
}
Catch (Exception e ){
System. out. println ("Directory creation error ");
E. printStackTrace ();
}
 
2. Create a file
Java code
File myFilePath = new File (% 1 );
Try {
If (! MyFilePath. exists ()){
MyFilePath. createNewFile ();
}
FileWriter resultFile = new FileWriter (myFilePath );
PrintWriter myFile = new PrintWriter (resultFile );
MyFile. println (% 2 );
ResultFile. close ();
}
Catch (Exception e ){
System. out. println ("file creation error ");
E. printStackTrace ();
}
 
 
3. delete an object
Java code
File myDelFile = new File (% 1 );
Try {
MyDelFile. delete ();
}
Catch (Exception e ){
System. out. println ("File Deletion error ");
E. printStackTrace ();
}
 
 
4. delete folders
Java code
File delFolderPath = new File (% 1 );
Try {
DelFolderPath. delete (); // delete an empty folder
}
Catch (Exception e ){
System. out. println ("Folder deletion error ");
E. printStackTrace ();
}
 
 
5. Delete all folders under a file
Java code
File delfile = new File (% 1 );
File [] files = delfile. listFiles ();
For (int I = 0; I <files. length; I ++ ){
If (files. isDirectory ()){
Files. delete ();
}
}
 
6. Clear folders
Java code
File delfilefolder = new File (% 1 );
Try {
If (! Delfilefolder. exists ()){
Delfilefolder. delete ();
}
Delfilefolder. mkdir ();
}
Catch (Exception e ){
System. out. println ("An error occurred while clearing the directory ");
E. printStackTrace ();
}
 
 
7. Read files
Java code
// Read data row by row
FileReader fr = new FileReader (% 1 );
BufferedReader br = new BufferedReader (fr );
String % 2 = br. readLine ();
While (% 2! = Null ){
% 3
% 2 = br. readLine ();
}
Br. close ();
Fr. close ();
 
 
8. Write files
Java code
// Write data to a file
FileWriter fw = new FileWriter (% 1 );
Fw. write (% 2 );
Fw. close ();
 
 
9. Write random files
Java code
Try {
RandomAcessFile logFile = new RandomAcessFile (% 1, "rw ");
Long lg = logFile. length ();
LogFile. seek (% 2 );
LogFile. writeByte (% 3 );
} Catch (IOException ioe ){
System. out. println ("file cannot be written:" + ioe. getMessage ());
}
 
 
10. Read File Attributes
Java code
// Obtain file attributes
File af = new File (% 1 );
If (af. exists ()){
The attributes of System. out. println (f. getName () + "are as follows:" + f. length ());
System. out. println (f. isFile ()? "Is a file": "Not a file ");
System. out. println (f. isDirectory ()? "Is a directory": "Not a directory ");
System. out. println (f. canRead ()? "Readable": "no ");
System. out. println (f. canWrite ()? "Hidden file ":"");
System. out. println ("the last modification Date of the folder is:" + new Date (f. lastModified ()));
} Else {
System. out. println (f. getName () + "attributes are as follows :");
System. out. println (f. isFile ()? "Is a file": "Not a file ");
System. out. println (f. isDirectory ()? "Is a directory": "Not a directory ");
System. out. println (f. canRead ()? "Readable": "no ");
System. out. println (f. canWrite ()? "Hidden file ":"");
System. out. println ("the last modification Date of the file is:" + new Date (f. lastModified ()));
}
If (f. canRead ()){
% 2
}
If (f. canWrite ()){
% 3
}
 
 
11. Write attributes
Java code
File filereadonly = new File (% 1 );
Try {
Boolean B = filereadonly. setReadOnly ();
}
Catch (Exception e ){
System. out. println ("Drop write access:" + e. printStackTrace ());
}
 
 
12. enumerate all folders in a folder
Java code
// Import java. io .*;
// Import java. util .*;
ArrayList <String> folderList = new ArrayList <String> ();
FolderList. add (% 1 );
For (int j = 0; j <folderList. size (); j ++ ){
File file = new File (folderList. get (j ));
File [] files = file. listFiles ();
ArrayList <File> fileList = new ArrayList <File> ();
For (int I = 0; I <files. length; I ++ ){
If (files. isDirectory ()){
FolderList. add (files. getPath ());
} Else {
FileList. add (files );
}
}
For (File f: fileList ){
% 2 = f. toString ();
% 3
}
}
 
13. Copy a folder
Java code
// Import java. io .*;
// Import java. util .*;
ArrayList <String> folderList = new ArrayList <String> ();
FolderList. add (% 1 );
ArrayList <String> folderList2 = new ArrayList <String> ();
FolderList2.add (% 2 );
For (int j = 0; j <folderList. length; j ++ ){
(New File (folderList2 [j]). mkdirs (); // create a new folder if the folder does not exist
File folders = new File (folderList [j]);
String [] file = folders. list ();
File temp = null;
Try {
For (int I = 0; I <file. length; I ++ ){
If (folderList [j]. endsWith (File. separator )){
Temp = new File (folderList [j] + "/" + file );
} & Nb

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.