---- MD5加密与生成UUID例子(依赖于commons-io.jar):begin ----------------------------------------------------------------------------------------------------
package com.mengyao.jdoc.commons; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.id.Hex;
import org.apache.commons.id.uuid.UUID; /**
*
* @author mengyao
*
*/
public class Utils { /**
* 使用MD5单向加密方式加密指定字符串
* @param str 待转换字符串
* 依赖于commons-codec.jar实现
* @return
*/
public static String getMD5Str(String str){
return DigestUtils.md5Hex(str);
} /**
* 获取UUID
* 依赖于apache-commons-id.jar、commons-discovery.jar实现
* @return
*/
public static String getUUID() {
return new String(Hex.encodeHex(UUID.randomUUID().getRawBytes()));
} public static void main(String[] args) {
System.out.println(getUUID());
System.out.println(getMD5Str("123456"));
}
}
---- MD5加密与生成UUID例子(依赖于commons-io.jar):end----------------------------------------------------------------------------------------------------
---- 监控文件变化例子(依赖于commons-io.jar):begin ----------------------------------------------------------------------------------------------------
package com.lefukj.parser.watcher; import java.io.File; import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver; /**
*
* @author mengyao
*
*/
public class FileListener extends FileAlterationListenerAdaptor { /**
* 文件创建执行
*/
@Override
public void onFileCreate(File file) {
System.out.println("[新建]:" + file.getAbsolutePath());
} /**
* 文件创建修改
*/
@Override
public void onFileChange(File file) {
System.out.println("[修改]:" + file.getAbsolutePath());
} /**
* 文件删除
*/
@Override
public void onFileDelete(File file) {
System.out.println("[删除]:" + file.getAbsolutePath());
} /**
* 目录创建
*/
@Override
public void onDirectoryCreate(File directory) {
System.out.println("[新建]:" + directory.getAbsolutePath());
} /**
* 目录修改
*/
@Override
public void onDirectoryChange(File directory) {
System.out.println("[修改]:" + directory.getAbsolutePath());
} /**
* 目录删除
*/
@Override
public void onDirectoryDelete(File directory) {
System.out.println("[删除]:" + directory.getAbsolutePath());
} @Override
public void onStart(FileAlterationObserver observer) {
super.onStart(observer);
} @Override
public void onStop(FileAlterationObserver observer) {
super.onStop(observer);
} } package com.lefukj.parser.watcher; import java.util.concurrent.TimeUnit; import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver; /**
*
* @author mengyao
*
*/
public class FileMonitorTest { /**
* @param args
*/
public static void main(String[] args) throws Exception {
// 监控目录
String rootDir = "D:/caiji";
// 轮询间隔1 秒
long interval = TimeUnit.SECONDS.toMillis(1);
// 创建一个文件观察器用于处理文件的格式
FileAlterationObserver observer = new FileAlterationObserver(rootDir,
FileFilterUtils.and(FileFilterUtils.fileFileFilter(),
FileFilterUtils.suffixFileFilter(".dat")), // 过滤文件格式
null);
// FileAlterationObserver _observer = new
// FileAlterationObserver(rootDir); observer.addListener(new FileListener()); // 设置文件变化监听器
// 创建文件变化监听器
FileAlterationMonitor monitor = new FileAlterationMonitor(interval,
observer);
// 开始监控
monitor.start();
} }
---- 监控文件变化例子(依赖于commons-io.jar):end ----------------------------------------------------------------------------------------------------
---- 文件读写操作例子(依赖于commons-io.jar):begin ----------------------------------------------------------------------------------------------------
package com.lefukj.parser.utils; import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.SystemUtils; import com.lefukj.parser.configuration.Configuration; /**
*
* @author mengyao
*
*/
@SuppressWarnings("all")
public class FileUtil { public static String getPath(){
String os = SystemUtils.OS_NAME;
String path = null;
if (os.contains("Windows") || os.contains("windows")) {
path = Configuration.WINDOWS_FILE_DIR;
} else if (os.contains("Linux") || os.contains("linux")) {
path = Configuration.LINUX_FILE_DIR;
} return path;
} public static File getFile(String path, String fileName){
File file = new File(path);
if(!file.isDirectory()){
file.mkdir();
}
file = new File(path+fileName);
return file;
} public static boolean writeFile(String path, String fileName, List<?> lines, boolean append) {
boolean status = false;
FileWriter writer = null;
try {
File file = new File(path);
if(!file.isDirectory()){
file.mkdir();
}
file = new File(path+fileName);
writer = new FileWriter(file, append);
IOUtils.writeLines(lines, null, writer);
status = true;
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
} return status;
} public static List<String> readFile(String path){
FileUtils fileUtils = new FileUtils();
List<String> readLines = null;
try {
File file = new File(path);
readLines = fileUtils.readLines(file);
} catch (IOException e) {
}
return readLines;
} public static long getFileLastUpdateTime(File file){
long lastModified = file.lastModified(); return lastModified;
} public static void main(String[] args) {
File file = new File("D:/caiji/users.dat");
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified())));
//2015-07-25 20:30:37
//2015-07-25 20:44:01
} }
---- 文件读写操作例子(依赖于commons-io.jar):end ----------------------------------------------------------------------------------------------------