Package Com.green.project.update.io;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.FileWriter;
Import Java.io.FilenameFilter;
Import Java.io.InputStream;
Import Java.io.PrintWriter;
Import java.util.ArrayList;
Import java.util.List;
public class Fileoperator {
/**
* New Directory
*
* @param folderpath
* String such as C:/FQF
* @return Boolean
*/
public static void NewFolder (String folderpath) {
try {
String FilePath = FolderPath;
FilePath = Filepath.tostring ();
File Myfilepath = new file (FilePath);
if (!myfilepath.exists ()) {
Myfilepath.mkdir ();
}
} catch (Exception e) {
SYSTEM.OUT.PRINTLN ("New directory Operation error");
E.printstacktrace ();
}
}
/**
* New File
*
* @param filepathandname
* String file path and name such as C:/fqf.txt
* @param filecontent
* String File contents
* @return Boolean
*/
public static 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 ("New directory Operation error");
E.printstacktrace ();
}
}
/**
* Delete Files
*
* @param filepathandname
* String file path and name such as C:/fqf.txt
* @param filecontent
* String
* @return Boolean
*/
public static void Delfile (String filepathandname) {
try {
String FilePath = filepathandname;
FilePath = Filepath.tostring ();
File Mydelfile = new file (FilePath);
Mydelfile.delete ();
} catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Error deleting file operation");
E.printstacktrace ();
}
}
/**
* Delete Folder
*
* @param filepathandname
* String folder path and name such as C:/FQF
* @param filecontent
* String
* @return Boolean
*/
public static void Delfolder (String folderpath) {
try {
Delallfile (FolderPath); Delete all content inside
String FilePath = FolderPath;
FilePath = Filepath.tostring ();
File Myfilepath = new file (FilePath);
Myfilepath.delete (); Delete Empty folders
} catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Error deleting folder operation");
E.printstacktrace ();
}
}
/**
* Delete all files inside the folder
*
* @param path
* String folder path such as C:/fqf
*/
public static 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 empty folder
}
}
}
/**
* Copy individual files
*
* @param OldPath
* String original file path such as: C:/fqf.txt
* @param NewPath
* String copied after path such as: F:/fqf.txt
* @return Boolean
*/
public static void CopyFile (String oldpath, String NewPath) {
try {
int bytesum = 0;
int byteread = 0;
File Oldfile = new file (OldPath);
if (oldfile.exists ()) {//File exists
InputStream instream = new FileInputStream (OldPath); Read in the original file
FileOutputStream fs = new FileOutputStream (NewPath);
byte[] buffer = new byte[1444];
int length;
while ((Byteread = instream.read (buffer))! =-1) {
Bytesum + = Byteread; Number of bytes File size
System.out.println (bytesum);
Fs.write (buffer, 0, byteread);
}
Instream.close ();
}
} catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Error copying Single file operation");
E.printstacktrace ();
}
}
/**
* Copy entire folder contents
*
* @param OldPath
* String original file path such as: C:/FQF
* @param NewPath
* String copied after path such as: F:/FQF/FF
* @return Boolean
*/
public static 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[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 ("Error copying entire folder contents operation");
E.printstacktrace ();
}
}
/**
* Move files to the specified directory
*
* @param OldPath
* String such as: C:/fqf.txt
* @param NewPath
* String such as: D:/fqf.txt
*/
public static void MoveFile (String oldpath, String NewPath) {
CopyFile (OldPath, NewPath);
Delfile (OldPath);
}
/**
* Move files to the specified directory
*
* @param OldPath
* String such as: C:/fqf.txt
* @param NewPath
* String such as: D:/fqf.txt
*/
public static void MoveFolder (String oldpath, String NewPath) {
CopyFolder (OldPath, NewPath);
Delfolder (OldPath);
}
/*
* Get all Files under folder
*
* @param file name
*
* @return All Files list
*/
public static list<string> files (String path) {
File File = new file (path);
if (!file.isdirectory ()) {
return null;
}
list<string> files = new arraylist<string> ();
For (File s:file.listfiles ()) {
if (S.isfile ()) {
Files.add (S.getname ());
}
}
return files;
}
/*
* Get all Files under folder
*
* @param file name
*
* @return All Files list
*/
public static list<string> Filesanddirs (String path) {
File File = new file (path);
if (!file.isdirectory ()) {
return null;
}
list<string> files = new arraylist<string> ();
For (File s:file.listfiles ()) {
Files.add (S.getname ());
}
return files;
}
/*
* Get files specified under folder
*
* @param file name
*
* @param suffix Name
*
* @return All Files list
*/
public static list<string> Findsbysuffix (string path, final string suffix) {
File File = new file (path);
if (!file.isdirectory ()) {
return null;
}
list<string> files = new arraylist<string> ();
For (File s:file.listfiles ()) {
if (S.getname (). EndsWith (suffix)) {
Files.add (S.getname ());
}
}
return files;
}
/**
* Copy the contents of the entire folder (including itself)
*
* @param OldPath
* Prepare a copy of the directory
* @param NewPath
* Specify a new directory for absolute paths
* @return
*/
public static void Copyfolderwithself (String oldpath, String NewPath) {
try {
New File (NewPath). Mkdirs (); Create a new folder if the folder does not exist
File dir = new file (OldPath);
Goal
NewPath + = File.separator + Dir.getname ();
File Movedir = new file (NewPath);
if (Dir.isdirectory ()) {
if (!movedir.exists ()) {
Movedir.mkdirs ();
}
}
string[] File = Dir.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[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
Copyfolderwithself (OldPath + "/" + file[i], NewPath);
}
}
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Copy a single file (can rename a copy)
*
* @param oldpathfile
* Ready to copy the file source
* @param newpathfile
* Copy to new absolute path with file name (Note: directory Path requires file name)
* @return
*/
public static void Copysinglefile (String oldpathfile, String newpathfile) {
try {
int bytesum = 0;
int byteread = 0;
File Oldfile = new file (oldpathfile);
if (oldfile.exists ()) {//File exists
InputStream instream = new FileInputStream (oldpathfile);//Read in original file
FileOutputStream fs = new FileOutputStream (newpathfile);
byte[] buffer = new byte[1444];
while (Byteread = instream.read (buffer))! =-1) {
Bytesum + = byteread;//bytes File size
//SYSTEM.OUT.PRINTLN (Bytesu m);
Fs.write (buffer, 0, byteread);
}
Instream.close ();
}
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Copy a single file (formerly copied)
*
* @param oldpathfile
* Ready to copy the file source
* @param newpathfile
* Copy to new absolute path with file name (Note: directory path Path required with file name)
* @return
*/
public static void Copysinglefileto (String oldpathfile, String targetpath) {
try {
int bytesum = 0;
int byteread = 0;
File Oldfile = new file (oldpathfile);
String targetfile = targetpath + File.separator + oldfile.getname ();
if (oldfile.exists ()) {//File exists
InputStream instream = new FileInputStream (oldpathfile);//Read in original file
FileOutputStream fs = new FileOutputStream (targetfile);
byte[] buffer = new byte[1444];
while (Byteread = instream.read (buffer))! =-1) {
Bytesum + = byteread;//bytes File size
//SYSTEM.OUT.PRINTLN (Bytesu m);
Fs.write (buffer, 0, byteread);
}
Instream.close ();
}
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Move the specified file or folder (including all files and subfolders)
*
* @param fromdir
* Files or folders to move
* @param todir
* Destination Folder
* @throws Exception
*/
public static void Movefolderandfilewithself (string from, string to)
Throws Exception {
try {
File dir = new file (from);
Goal
to + = File.separator + Dir.getname ();
File Movedir = new file (to);
if (Dir.isdirectory ()) {
if (!movedir.exists ()) {
Movedir.mkdirs ();
}
} else {
File ToFile = new file (to);
Dir.renameto (ToFile);
Return
}
System.out.println ("Dir.isdirectory ()" +dir.isdirectory ());
System.out.println ("Dir.isfile ():" +dir.isfile ());
List of documents
file[] files = dir.listfiles ();
if (files = = null)
Return
File move
for (int i = 0; i < files.length; i++) {
System.out.println ("File name:" + files[i].getname ());
if (Files[i].isdirectory ()) {
Movefolderandfilewithself (Files[i].getpath (), to);
Successful, delete original file
Files[i].delete ();
}
File MoveFile = new file (Movedir.getpath () + File.separator
+ Files[i].getname ());
If the destination folder exists, delete the
if (movefile.exists ()) {
Movefile.delete ();
}
Files[i].renameto (MoveFile);
}
Dir.delete ();
} catch (Exception e) {
Throw e;
}
}
To delete a folder recursively
public static void DeleteFile (String path) {
File File=new file (path);
if (file.exists ()) {//Determine if the file exists
if (File.isfile ()) {//Determine if the file is
File.delete ();//delete files
} else if (File.isdirectory ()) {//Otherwise if it is a directory
file[] files = file.listfiles ();//Declare all files under the directory files[];
for (int i = 0;i < Files.length;i + +) {//traverse directory for all Files
DeleteFile (Files[i].getabsolutepath ());//iterate over each file in this way
}
File.delete ();//Delete folder
}
} else {
System.out.println ("The deleted file does not exist");
}
}
/*
* Determine if a file or folder exists
*
* @param: Absolute path
*
* @param: true, exists. False does not exist
*/
public static Boolean isexist (String path) {
File File=new file (path);
if (File.exists ())
return true;
return false;
}
}
Operation of the file