public class FileOperations {

/**

* 验证文件是否存在目录中

* @param targetPath 目标文件路径,包含文件全名称,包含后缀

* @return 0 默认,1 参数错误,2 存在文件,3 不存在文件

*/

public int checkFileExists(String targetPath){

int result = 0;

if(targetPath == null || "".equals(targetPath) || targetPath.indexOf(".") <= 0){

l.info("<<== check File Exists -- path Error : "+ targetPath);

return 1;

}

try{

File checkFile = new File(targetPath);

if(checkFile.exists()){

result = 2;

}else{

result = 3;

}

}catch(Exception e){

l.error("<<== check File Exists Exception : "+ e);

}

return result;

}


/**

* 删除目标目录下文件

* @param targetPath 目标文件路径,包含文件全名称,包含后缀

* @return 0 默认,1 参数错误,2 删除文件成功,3 删除文件失败

*/

public int deleteFile(String targetPath) {

int result = 0;

if(targetPath == null || "".equals(targetPath) || targetPath.indexOf(".") <= 0){

l.info("<<== delete File -- path Error : "+ targetPath);

return 1;

}

try {

File delFile = new File(targetPath);

if (delFile.delete()) {

result = 2;

}else{

result = 3;

}

} catch (Exception e) {

l.error("<<== delete File Exception : " + e);

}

return result;

}

/**

* 更换目标目录下文件名称

* @param targetPath 目标文件路径,及要操作的文件目录

* @param oldName 要更改的文件名

* @param newName 更改为的文件名

* @return 0 默认,1 参数错误,2 更改文件名称成功,3 更改文件名称失败

*/

public int reNameFile(String targetPath, String oldName, String newName) {

int result = 0;


if (targetPath == null || "".equals(targetPath) || oldName == null || "".equals(oldName) || oldName.indexOf(".") <= 0 || newName == null || "".equals(newName) || newName.indexOf(".") <= 0 ) {

l.info("<<== reName File -- param Error! targetPath=" + targetPath + "--oldName=" + oldName + "--newName=" + newName);

return 1;

}

StringBuffer strBuff = new StringBuffer();

try {

//获得源文件流

strBuff.delete(0, strBuff.length());

strBuff.append(targetPath).append(oldName);

File reNameFile = new File(strBuff.toString());


//获得更改文件流

strBuff.delete(0, strBuff.length());

strBuff.append(targetPath).append(newName);

File newNameFile = new File(strBuff.toString());

//执行更改,返回更改结果

if(reNameFile.renameTo(newNameFile)) {

result = 2;

}else{

result = 3;

}

} catch(SecurityException e){

l.error("<<== file Operation Is Limited!--SecurityException : " + e);

} catch (Exception e) {

l.error("<<== reName File Exception : " + e);

}


return result;

}

/**

* 获得文件后缀

* @param fileAllName 文件全名称

* @return 文件后缀

*/

public String getSuffix(String fileAllName,String defaultSuffix){

String suffix = defaultSuffix;

if(fileAllName == null || "".equals(fileAllName) || fileAllName.lastIndexOf(".") <= 0){

l.info("<<== getSuffix fileAllName Error! fileAllName=" + fileAllName);

return suffix;

}

suffix = fileAllName.substring(fileAllName.lastIndexOf("."));

return suffix;

}

/**

* 将文件存储到缓存文件夹

* @param fileAllName 文件全名称,包含文件名

* @param fileCount 所要写入文件的内容

* @return 是否创建文件,并成功写入内容

*/

public boolean putFileToCahce(String fileAllName,StringBuilder fileCount){

boolean result = false;

StringBuffer cachePath = new StringBuffer().append(XingbookConfig.sto_path).append(XingbookConfig.icon_srcPath).append(fileAllName);

String targetPath = cachePath.toString();//完整存储名称

if(checkFileExists(targetPath) == 2){

if(deleteFile(targetPath) == 2){

l.info("<<== file is have,delete it success!");

}

}

result = FileUtil.writeFile(targetPath, fileCount, "UTF-8");

return result;

}

/**

* 从指定地点读取文件内容

* @param fileName 文件全路径

* @return String 读取内容串,null 读取失败

*/

public static String readTextFile(String fileName) {

File file = new File(fileName);

       BufferedReader reader = null;

       StringBuffer out = null;

       try {

           reader = new BufferedReader(new FileReader(file));

           String tmp = null;

           out = new StringBuffer();

           while ((tmp = reader.readLine()) != null) {

out.append(tmp);

           }

           reader.close();

       } catch (IOException e) {

           out = null;

       } finally {

           if (reader != null) {

               try {

                   reader.close();

               } catch (IOException e1) {

l.info("<<== read text file is error!");

               }

           }

       }

       if (out != null) {

return out.toString();

       }

return null;

}

/**

* 从指定地点读取文件大小

* @param fileName 文件全路径

* @return >0:文件真实大小(单位K), 0:错误;

*/

public long getFileSize(String fileName){

if(checkFileExists(fileName) == 2){

File file = new File(fileName);

long size = file.length();

if(size > 0){

return size;

}

}

return 0;

}