Java Operations files

Source: Internet
Author: User
Tags log log

/*

*/
Package com.***.app.mappcore.impl.util;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.nio.channels.FileChannel;

Import Com.ygsoft.ecp.service.log.EcpLogFactory;
Import Com.ygsoft.ecp.service.log.IEcpLog;
Import Com.ygsoft.ecp.service.tool.StringUtil;

/**
* File Processing .<br>
*
* @author Mapengfei <br>
* @version 1.0.0 April 14, 2016 <br>
* @see
* @since JDK 1.5.0
*/
public class Forderutil {
/**
* Log
*/
private static final Iecplog LOG = Ecplogfactory.getlog (Forderutil.class);

/**
* The base path of the service.
*/
private static String Base_path = Systemconfigutil.get ("Server_base_home", "FTP") + "\\ECPCLOUDSERVER\\WLP";
/**
* The path of the service node.
*/
private static String Server_path = Base_path + "\\usr\\servers";

/**
* Deletes the specified directory or file according to the path, regardless of whether it exists or not
*
* @param spath
* The directory or file to be deleted
* @return Delete succeeds returns True, otherwise false is returned.
*/
public static Boolean DeleteFolder (final String spath) {
Boolean flag = false;
File File = new file (spath);
Determine if a directory or file exists
if (!file.exists ()) {//does not exist return false
return flag;
} else {
Determine if the file is
if (File.isfile ()) {//is a file when the delete file method is called
return DeleteFile (spath);
The Delete directory method is called when the else {//is a directory
return DeleteDirectory (spath);
}
}
}

/**
* Delete directories (folders) and files in directory
*
* @param spath
* The file path of the deleted directory
* @return Directory Delete successfully returns TRUE, otherwise false
*/
private static Boolean deletedirectory (String spath) {
If Spath does not end with a file delimiter, the file delimiter is automatically added
if (!spath.endswith (File.separator)) {
spath = spath + file.separator;
}
File Dirfile = new file (spath);
If the dir corresponding file does not exist, or is not a directory, exit
if (!dirfile.exists () | |!dirfile.isdirectory ()) {
return false;
}
Boolean flag = true;
Delete all files (including subdirectories) under a folder
file[] files = dirfile.listfiles ();
for (int i = 0; i < files.length; i++) {
Delete sub-Files
if (Files[i].isfile ()) {
Flag = DeleteFile (Files[i].getabsolutepath ());
if (!flag)
Break
}//Delete sub-directories
else {
Flag = DeleteDirectory (Files[i].getabsolutepath ());
if (!flag)
Break
}
}
if (!flag)
return false;
Delete current directory
if (Dirfile.delete ()) {
return true;
} else {
return false;
}
}

/**
* Delete individual files
*
* @param spath
* File name of deleted files
* @return Single File Delete successfully returns TRUE, otherwise false
*/
private static Boolean DeleteFile (final String spath) {
Boolean flag = false;
File File = new file (spath);
The path is a file and is not empty to delete
if (File.isfile () && file.exists ()) {
File.delete ();
Flag = true;
}
return flag;
}

/**
* File Channel replication scheme
*
* @param s
* @param t
*/
public static void Filechannelcopy (final file from, final file to) {
File input stream
FileInputStream fi = null;
File output stream
FileOutputStream fo = null;
Input channel
FileChannel in = null;
Output channel
FileChannel out = null;
try {
Read stream
fi = new FileInputStream (from);
Output stream
fo = new FileOutputStream (to);
Get file Stream Channel
in = Fi.getchannel ();
out = Fo.getchannel ();
Channel connection
In.transferto (0, In.size (), out);
} catch (FileNotFoundException e) {
Create a read stream exception
E.printstacktrace ();
} catch (IOException e) {
Channel Connectivity Anomalies
E.printstacktrace ();
} finally {
try {
Fi.close ();
In.close ();
Fo.close ();
Out.close ();
} catch (IOException e) {
Stream Close exception
E.printstacktrace ();
}

}
}

/**
* Copy files and directories from Frompath to Topath
*
* @param frompath
* Parent File path
* @param topath
* Destination file path
* @param iszip
* Compression Marking
* @param Zippath
* Zip Compressed path (this parameter is used in conjunction with Iszip)
*/
public static void CopyFile (String frompath, String topath, Final String Zippath, final Boolean iszip) {
If Spath does not end with a file delimiter, the file delimiter is automatically added
if (!frompath.endswith (File.separator)) {
Frompath = Frompath + file.separator;
}
File Dirfile = new file (Frompath);
Mkdirsfilebypath (Topath);
if (Dirfile.isdirectory ()) {
Copy all files (including subdirectories) under the folder
file[] files = dirfile.listfiles ();
for (int i = 0; i < files.length; i++) {
File f = files[i];
If the file
if (F.isfile ()) {
File ToFile = new file (topath + "\ \" + f.getname ());
Filechannelcopy (f, tofile);
} else {
Topath = Topath + "\ \" + F.getname ();
Mkdirsfilebypath (Topath);
}
}
} else {
File ToFile = new file (topath + "\ \" + dirfile.getname ());
Filechannelcopy (Dirfile, tofile);

}
if (Iszip && stringutil.isnotemptystring (Zippath)) {
try {
if (log.isdebugenabled ()) {
Log.debug ("Start compressing the log file, the compressed file path is" + Frompath + "\ \" + "Log.zip");
}
File ZipFile = new file (Zippath);
if (zipfile.exists ()) {
Forderutil.deletefolder (Zippath);
}
Zipmultifile.zip (Zippath, "", Topath);
Forderutil.deletefolder (Topath);
} catch (Exception e) {
if (log.isdebugenabled ()) {
Log.info ("Forderutil.copyfile failed during file compression");
}
E.printstacktrace ();
}
} else {
if (log.isdebugenabled ()) {
Log.info ("Forderutil.copyfile method compression marked as false or compressed path is null" + Zippath);
}
}
}

/**
* Get log Log compressed file
*
* @param Zippath
* @param mappname
* @return
*/
public static string Getlogzip (final String mappname) {
String Topath = server_path + "\ \" + Mappname + "\\log";
String Frompath = server_path + "\ \" + Mappname + "\\logs\\console.log";
String Zippath = server_path + "\ \" + Mappname + "\\log.zip";
CopyFile (Frompath, Topath, Zippath, true);
return zippath;
}

/**
* Automatically create path
*
* @param path
* @return
*/
private static void Mkdirsfilebypath (final String path) {
File ToFile = new file (path);
if (!tofile.exists ()) {
Tofile.mkdirs ();
}
}

public static void Main (final string[] args) throws Exception {
String from = "D:\\ygecpcloud\\ecpcloudserver\\wlp\\usr\\servers\\mappstore\\logs";
String to = "D:\\ygecpcloud\\ecpcloudserver\\wlp\\usr\\servers\\mappstore\\log";
CopyFile (from, To, "D:\\ygecpcloud\\ecpcloudserver\\wlp\\usr\\servers\\mappstore\\l.zip", true);
}

}

Java Operations files

Related Article

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.