Import java.io.*;
public class Fileoperate {
Public Fileoperate () {
}
/**
* New directory
* @param folderpath String such as C:/FQF
* @return bool EAN
*
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 ("Error in new directory operation");
e.printstacktrace ();
}
}
/**
* New file
* @param filepathandname String file path and name such as C:/fqf.txt
; * @param filecontent String file content
* @return Boolean
* *
public void NewFile (STR ing 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 File
* @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 ("Delete file operation error");
E.printstacktrace ();
}
}
/**
* Delete 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 inside
String FilePath = FolderPath;
FilePath = Filepath.tostring ();
Java.io.File Myfilepath = new Java.io.File (FilePath);
Myfilepath.delete (); Delete empty Folder
}
catch (Exception e) {
System.out.println ("Delete folder operation Error");
E.printstacktrace ();
}
}
/**
* Delete all files inside the folder
* @param path String folder paths 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 files in 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 Copy the path after: 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 ()) {//File exists
InputStream instream = new FileInputStream (OldPath); Read into the original file
FileOutputStream fs = new FileOutputStream (NewPath);
byte[] buffer = new byte[1444];
int length;
while ((Byteread = instream.read (buffer))!=-1) {
Bytesum + = Byteread; Byte Number file size
System.out.println (bytesum);
Fs.write (buffer, 0, byteread);
}
Instream.close ();
}
}
catch (Exception e) {
System.out.println ("Copy single File operation Error");
E.printstacktrace ();
}
}
/**
* Copy entire folder contents
* @param oldpath String original file path such as: C:/FQF
* @param newpath String Copy the path after: 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[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 ("Copy entire folder contents operation Error");
E.printstacktrace ();
}
}
/**
* Move files to 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 files to 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);
}
}