Operate file of java based on java file

Source: Internet
Author: User

Java File Operations
Copy codeThe Code is as follows: package com. b510;

Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. FileWriter;
Import java. io. InputStream;
Import java. io. PrintWriter;

/**
*
* @ Author Hongten </br>
*
* File Operations
*
*
*
*/
Public class OperateFiles {

/**
* @ Param args
*/
Public static void main (String [] args ){
OperateFiles operateFiles = new OperateFiles ();
// Create a folder
OperateFiles. newFolder ("c:/hongten ");
// Create a new file and write content to it
OperateFiles. newFile ("c:/hongten/Hello.txt", "hello, I'm Hongten. hello ");
// Delete an object
OperateFiles. deleteFile ("c:/hongten/Hello.txt ");
// Delete a folder
OperateFiles. deleteFolder ("c:/hongten ");
// Copy a folder
OperateFiles. copyFolder ("c:/hongten", "e:/hongten ");
// Extract the file extension
String expandedName = operateFiles. getExpandedName ("c:/hongten/Hello.txt ");
System. out. println (expandedName );
// Extract the file path
System. out. println (operateFiles. getFilePath ("c:/hongten/Hello.txt "));
}

/**
* Get the file extension
* @ Param filePath: the file path is c:/hongten/Hello.txt.
* @ Return: the file extension is txt.
*/
Public String getExpandedName (String filePath ){
Return filePath. substring (filePath. lastIndexOf (".") + 1 );
}
/**
* Obtain the file path
* @ Param file path
* @ Return file path
*/
Public String getFilePath (String file ){
Return file. substring (0, file. lastIndexOf ("/"));
}
/**
* Create a directory
*
* @ Param folderPath
* Directory Creation Path: c: \ newFolder
*/
Public void newFolder (String folderPath ){
Try {
File myFolderPath = new File (folderPath. toString ());
If (! MyFolderPath. exists ()){
MyFolderPath. mkdir ();
}
} Catch (Exception e ){
System. out. println ("Directory creation error ");
E. printStackTrace ();
}
}

/**
* Create a file
*
* @ Param filePath
* Directory of the new file, for example, c: \ hongten. java
*/
Public void newFile (String filePath ){
Try {
File myFilePathFile = new File (filePath. toString ());
If (! MyFilePathFile. exists ()){
MyFilePathFile. createNewFile ();
}
} Catch (Exception e ){
System. out. println ("failed to create new file ");
E. printStackTrace ();
}
}

/**
* Create a new file and write content to the file
*
* @ Param filePath
* Directory of the new file, for example, c: \ hongten. java
* @ Param fileContent
* Content written to the file
*/
Public void newFile (String filePath, String fileContent ){
Try {
NewFile (filePath );
FileWriter resultFile = new FileWriter (filePath );
PrintWriter myFile = new PrintWriter (resultFile );
MyFile. println (fileContent );
ResultFile. close ();
} Catch (Exception e ){
System. out. println ("file creation error ");
E. printStackTrace ();
}
}

/**
* Delete an object
*
* @ Param filePath
* The absolute path of the file to be deleted, for example, c :\\ hongten \ Hello.txt
*/
Public void deleteFile (String filePath ){
Try {
File preDelFile = new File (filePath );
If (preDelFile. exists ()){
PreDelFile. delete ();
} Else {
System. out. println (filePath + "does not exist! ");
}
} Catch (Exception e ){
System. out. println ("File Deletion error ");
E. printStackTrace ();
}
}

/**
* Delete A folder
*
* @ Param folderPath
* The absolute path of the folder to be deleted, for example, c :\\ hongten
*/
Public void deleteFolder (String folderPath ){
Try {
DelAllFiles (folderPath );
File preDelFoder = new File (folderPath );
If (preDelFoder. exists ()){
PreDelFoder. delete ();
} Else {
System. out. println (folderPath + "does not exist! ");
}
} Catch (Exception e ){
System. out. println ("File Deletion error ");
E. printStackTrace ();
}
}

/**
* Delete all objects in a folder
*
* @ Param folderPath
* The absolute path of the folder to be deleted, for example, c :\\ hongten
*/
Public void delAllFiles (String folderPath ){
File file = new File (folderPath );
If (! File. exists ()){
Return;
}
If (! File. isDirectory ()){
Return;
}
String [] tempList = file. list ();
File temp = null;
For (int I = 0; I <tempList. length; I ++ ){
If (folderPath. endsWith (File. separator )){
Temp = new File (folderPath + tempList [I]);
} Else {
Temp = new File (folderPath + File. separator + tempList [I]);
}
If (temp. isFile ()){
Temp. delete ();
}
If (temp. isDirectory ()){
DelAllFiles (folderPath + "/" + tempList [I]); // delete the files in the folder first
DeleteFolder (folderPath + "/" + tempList [I]); // Delete the empty folder
}
}
}

/**
* Copy a single file
*
* @ Param oldPath
* Original path: c: \ Hello. java
* @ Param newPath
* The New Path is f: \ Hello. java.
*/
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 = 0;
While (byteread = inStream. read (buffer ))! =-1 ){
Bytesum + = byteread; // file size in bytes
Fs. write (buffer, 0, byteread );
}
InStream. close ();
}
} Catch (Exception e ){
System. out. println ("An error occurred while copying a single file ");
E. printStackTrace ();

}
}

/**
* Folder Replication
*
* @ Param oldPath
* The original folder path is c: \ hello.
* @ Param newPath
* The path of the new folder is e: \ hello.
*/
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 single file
*
* @ Param oldPath
* The source file path is c: \ hello. java.
* @ Param newPath
* The path of the new file is e: \ hello. java.
*/
Public void moveFile (String oldPath, String newPath ){
CopyFile (oldPath, newPath );
DeleteFile (oldPath );
}

/**
* Move a folder
*
* @ Param oldPath
* The original folder path is c: \ hongten.
* @ Param newPath
* The path of the new folder is e: \ hongten.
*/
Public void moveFolder (String oldPath, String newPath ){
CopyFolder (oldPath, newPath );
DeleteFolder (oldPath );
}

/**
* Obtain the absolute path of the system root directory
*
* @ Return
*/
Public String getPath (){
String sysPath = this. getClass (). getResource ("/"). getPath ();
// Modify the path
SysPath = sysPath. substring (1, sysPath. length ()-16 );
Return sysPath;
}

}

Now I have time to sort out these things and share them with you ......

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.