3

 public static Properties loadPropertiesFile(String fileName) {
Properties properties = null;
FileInputStream input = null;
InputStreamReader isr = null;
try {
properties = new Properties();
input = new FileInputStream(fileName);
isr = new InputStreamReader(input, "UTF-8");
properties.load(isr);
input.close();
} catch (IOException e1) {
// e1.printStackTrace();
// C_381619 System Information Leak
logger.error("loadPropertiesFile error,", e1);
} finally {
if (null != input) {
try {
input.close();
} catch (Exception e1) {
logger.error("loadPropertiesFile error,", e1);
}
}
if (null != isr) {
try {
isr.close();
} catch (Exception e1) {
logger.error("loadPropertiesFile error,", e1);
}
}
}
return properties;
}

public static void writeFile(String list, String fileName) {
create(fileName);
FileOutputStream fs = null;
try {
fs = new FileOutputStream(fileName);
fs.write(list.getBytes(Charset.forName("utf-8")));
fs.close();
fs = null;
} catch (Exception e) {
logger.error("writeFile error,", e);
} finally {
try {
if (fs != null)
fs.close();
} catch (Exception e) {
logger.error("writeFile error,", e);
}
}
}

/**
* 解压zip包
* @param targetFile
* @param desPath
* @return
*/
@SuppressWarnings("unchecked")
public static int unZipFile(File targetFile, String desPath)
{
// 用于记录压缩包中的文件数
int fileCount = 0;
String zipPattern = "^.+(.zip)$";
String fileName = targetFile.getAbsolutePath();
Pattern pattern = Pattern.compile(zipPattern);
Matcher matcher = pattern.matcher(fileName.toLowerCase(Locale.US));
if (!matcher.find()) {
// 非zip包,不需要解压,直接返回
return fileCount;
}

ZipFile zip = null;
try {

zip = new ZipFile(fileName,"gbk");
Enumeration<ZipEntry> en = zip.getEntries();
ZipEntry entry = null;
byte[] buffer = new byte[2048];
int length = 0;
InputStream input = null;
BufferedOutputStream bos = null;
File file = null;

// 遍历文件夹下的所有文件
while (en.hasMoreElements()) {

try {
// 如果是目录,判断目录是否存在,不存在,创建目录
entry = (ZipEntry) en.nextElement();
if (entry.isDirectory()) {
file = new File(desPath, entry.getName());
if (!file.exists()) {
if (!file.mkdirs()) {
logger.warn("file delete failed , file is ", file.getAbsolutePath());
}
}
continue;
}
// 如果是文件
input = zip.getInputStream(entry);
file = new File(desPath, entry.getName());

// 如果要存放的目的目录不存在,创建目录
if (!file.getParentFile().exists()) {
if (!file.getParentFile().mkdirs()) {
logger.warn("file delete failed , file is ", file.getParentFile().getAbsolutePath());
}
}
bos = new BufferedOutputStream(new FileOutputStream(file));
//OutputStream outputStream = Files.newOutputStream(new File("c:/tmp.txt").toPath(), CREATE, WRITE);
while (null != input && (length = input.read(buffer)) != -1) {
bos.write(buffer, 0, length);
}

pattern = Pattern.compile(zipPattern);
matcher = pattern.matcher(file.getName().toLowerCase(Locale.US));
if (matcher.find()) {
fileCount++;
}
} catch (Exception e) {
logger.error("unZipFile loop process error, ", e);
} finally {
closeBufferedOutput(bos);
closeInputStream(input);
}
}

}
catch (FileNotFoundException e) {
logger.error("unZipFile: FileNotFoundException , and exception is:", e);
}
catch (IOException e) {
logger.error("unZipFile: IOException , and exception is:" , e);
}
catch (Exception e) {
logger.error("unZipFile: Exception, and exception is:" , e);
} finally {
closeZipFile(zip);
}

return fileCount;

}

public static void closeBufferedOutput(BufferedOutputStream bos) {
try {
if (null != bos) {
bos.close();
}
}
catch (IOException e) {
logger.error("closeBufferedOutput error, ", e);
}
}

public static void closeInputStream(InputStream input) {
try {
if (null != input) {
input.close();
}
}
catch (IOException e) {
logger.error("closeInputStream error, ", e);
}
}

public static void closeZipFile(ZipFile zip) {
try {
if (null != zip) {
zip.close();
}
}
catch (IOException e) {
logger.error("closeZipFile error, ", e);
}
}


/**
* 取得指定文件下的所有文件列表,包括子目录.
* @param baseDir
* File 指定的目录
* @return 包含java.io.File的List
*/
public static List<File> getSubFiles(File baseFile)
{
List<File> ret = new ArrayList<File>();
File[] tmp = baseFile.listFiles();
for (int i = 0; i < tmp.length; i++)
{
if (tmp[i].isFile())
{
ret.add(tmp[i]);
}
if (tmp[i].isDirectory())
{
ret.addAll(getSubFiles(tmp[i]));
}
}
return ret;
}

/**
* 删除文件或文件夹下所有子文件
* @param targetFile
* @return
*/
public static boolean deleteFiles(File targetFile) {
if (targetFile.isDirectory()) {
String[] children = targetFile.list();
if (null != children && children.length > 0) {
for (int i = 0; i < children.length; i++) {
boolean success = deleteFiles(new File(targetFile, children[i]));
if (!success) {
return false;
}
}
}
}
// 目录此时为空,可以删除
return targetFile.delete();
}



}
原文地址:https://www.cnblogs.com/sg9527/p/8568945.html