Java.io.file

Source: Internet
Author: User

Package Cn.edu.tongji.cims.wade.system;

Import java.io.*;

public class Fileoperate {
Public Fileoperate () {
}

/**
* New 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 ("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 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 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 ("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 void Delfolder (String folderpath) {
try {
Delallfile (FolderPath); Delete all content inside
String FilePath = FolderPath;
FilePath = Filepath.tostring ();
Java.io.File Myfilepath = new Java.io.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 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 the files in the folder first
Delfolder (path+ "/" + templist[i]);//delete empty folders again
}
}
}

/**
* Copy individual files
* @param oldpath String original file path such as: C:/fqf.txt
* @param NewPath String After copying the path such as: 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 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 After copying the path such as: 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 ("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 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 void MoveFolder (string oldpath, String NewPath) {
CopyFolder (OldPath, NewPath);
Delfolder (OldPath);

}
}

Java.io.file

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.