JAVAファイルを読み込み、最後に変更したファイルを並べ替えて取り出す
5827 ワード
フォルダを読み込んで巡回し、最新の変更時間を取得してソートします.
package com.util;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class IoUtil {
/**
*
* @param fileName
* @return
*/
public static File createFile(String filePathName)throws Exception{
File file = new File(filePathName);
if(!file.exists()){
file.createNewFile();
}else{
file.delete();
file.createNewFile();
}
return file;
}
/**
*
*
*/
public static boolean writeTxtFile(String content,String filePathName,String bytes)throws Exception{
boolean flag=false;
FileOutputStream o=null;
try {
String path="";
if(filePathName.indexOf("/")>-1){
path=filePathName.substring(0,filePathName.lastIndexOf("/"));
}else if(filePathName.indexOf("\\")>-1){
path=filePathName.substring(0,filePathName.lastIndexOf("\\"));
}
createDir(path);
o = new FileOutputStream(createFile(filePathName));
o.write(content.getBytes(bytes!=null&&!bytes.equals("")?bytes:"UTF-8"));
o.close();
flag=true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return flag;
}
/**
*
* UTF-8
*/
public static boolean writeTxtFile(String content,String filePathName)throws Exception{
boolean flag=false;
FileOutputStream o=null;
try {
String path="";
if(filePathName.indexOf("/")>-1){
path=filePathName.substring(0,filePathName.lastIndexOf("/"));
}else if(filePathName.indexOf("\\")>-1){
path=filePathName.substring(0,filePathName.lastIndexOf("\\"));
}
createDir(path);
o = new FileOutputStream(createFile(filePathName));
o.write(content.getBytes("UTF-8"));
o.close();
flag=true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return flag;
}
/**
* @param destDirName
* @return
*/
public static boolean createDir(String destDirName) {
File dir = new File(destDirName);
if (dir.exists()) {
return false;
}
if (!destDirName.endsWith(File.separator)) {
destDirName = destDirName + File.separator;
}
//
if (dir.mkdirs()) {
return true;
} else {
return false;
}
}
/**
* TEST
* @param args
*/
public static void main(String[] args) {
//Map<String,Object> map = new LinkedHashMap<String,Object>();
//map=mlistFiles(getPath()+"/test");
//System.out.println("===>"+getPath());
}
/**
*
*
* @return
*/
public static String getPath() {
String path = IoUtil.class.getResource("").getPath();
path = path.substring(0, path.indexOf("WEB-INF"));
return path;
}
/**
*
* @param path
* @return
*/
public static Map<String,Object> mlistFiles(String path){
File file_path=new File(path);
//
File[] files = file_path.listFiles();
//
Map<String,Object> map = new LinkedHashMap<String,Object>();
// ( ) ,
if(files!=null){
for(int i=0;i<files.length;i++){
if(!files[i].isHidden()&&files[i].isFile()&&!files[i].getName().equals("sitemap.xml")){
String value=files[i].getName();
String key=String.valueOf(files[i].lastModified());
key=getMilliseconds(key,i);
//System.out.println("======key=====>"+key);
map.put(key,value);
}
}
}
return orderFile(map.size(),map);
}
/**
* linux 000
* @param val
* @param i
* @return
*/
public static String getMilliseconds(String val,int i){
// 000
String returnStr="";
String lastStr=val.substring(val.length()-3,val.length());
if(lastStr.equals("000")){
String fisrtStr = val.substring(0,val.length()-3);
String lastNum="001";
if(i<10){
lastNum="00"+String.valueOf(i);
}else if(i<100){
lastNum="0"+String.valueOf(i);
}else{
lastNum=i<1000?String.valueOf(i):lastNum;
}
returnStr=fisrtStr+lastNum;
}else{
returnStr=val;
}
return returnStr;
}
/**
*
* @param x
* @param map
* @return
*/
public static Map<String,Object> orderFile(int x,Map<String,Object> map){
Map<String,Object> new_map = new LinkedHashMap<String,Object>();
Long[] arr = new Long[x];
int i=0;
Iterator<String> ite = map.keySet().iterator();
while(ite.hasNext()){
String key = ite.next();
arr[i] = Long.parseLong(key);
i++;
}
Arrays.sort(arr);
for(int k=arr.length-1;k>=0;k--){
//System.out.println("==>"+arr[k]+"-->"+dateTimeToStr(arr[k])+">>>"+map.get(""+arr[k]));
new_map.put(String.valueOf(arr[k]), map.get(""+arr[k]));
}
return new_map;
}
/**
*
* @param time
* @param format
* @return
*/
public static String dateTimeToStr(long time) {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(time);
return sf.format(date);
}
}