|
@@ -1,10 +1,13 @@ |
|
|
package cc.smtweb.framework.core.util; |
|
|
package cc.smtweb.framework.core.util; |
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.apache.commons.io.FileUtils; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
import java.io.FileInputStream; |
|
|
import java.io.FileInputStream; |
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.io.UnsupportedEncodingException; |
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
|
|
import java.nio.charset.Charset; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@@ -14,18 +17,26 @@ import java.nio.charset.StandardCharsets; |
|
|
@Slf4j |
|
|
@Slf4j |
|
|
public class FileUtil { |
|
|
public class FileUtil { |
|
|
/** |
|
|
/** |
|
|
|
|
|
* 文件fn是否存在 |
|
|
|
|
|
* |
|
|
|
|
|
* @param fn fileName |
|
|
|
|
|
* @return boolean |
|
|
|
|
|
*/ |
|
|
|
|
|
public static boolean isFileExist(String fn) { |
|
|
|
|
|
return !(fn == null || fn.length() < 1) && (new File(fn)).exists(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
* 从fn中读取所有内容 |
|
|
* 从fn中读取所有内容 |
|
|
* |
|
|
* |
|
|
* @param fn 源文件名 |
|
|
* @param fn 源文件名 |
|
|
* @return byte[] |
|
|
* @return byte[] |
|
|
*/ |
|
|
*/ |
|
|
public static byte[] readFileByte(String fn) { |
|
|
public static byte[] readFileByte(String fn) { |
|
|
|
|
|
File f = new File(fn); |
|
|
|
|
|
if (!f.exists()) return null; |
|
|
try { |
|
|
try { |
|
|
FileInputStream fis = new FileInputStream(fn); |
|
|
|
|
|
byte[] data = new byte[fis.available()]; |
|
|
|
|
|
fis.read(data, 0, data.length); |
|
|
|
|
|
fis.close(); |
|
|
|
|
|
return data; |
|
|
|
|
|
|
|
|
return FileUtils.readFileToByteArray(f); |
|
|
} catch (IOException e) { |
|
|
} catch (IOException e) { |
|
|
log.error(e.getMessage(), e); |
|
|
log.error(e.getMessage(), e); |
|
|
return null; |
|
|
return null; |
|
@@ -33,15 +44,15 @@ public class FileUtil { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static String readFileStr(String fn) { |
|
|
public static String readFileStr(String fn) { |
|
|
return readFileStr(fn, StandardCharsets.UTF_8.toString()); |
|
|
|
|
|
|
|
|
return readFileStr(fn, StandardCharsets.UTF_8); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static String readFileStr(String fn, String encode) { |
|
|
|
|
|
byte[] bytes = readFileByte(fn); |
|
|
|
|
|
if (bytes == null) return null; |
|
|
|
|
|
|
|
|
public static String readFileStr(String fn, Charset encode) { |
|
|
|
|
|
File f = new File(fn); |
|
|
|
|
|
if (!f.exists()) return null; |
|
|
try { |
|
|
try { |
|
|
return new String(bytes, encode); |
|
|
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
|
|
|
|
|
return FileUtils.readFileToString(f, encode); |
|
|
|
|
|
} catch (IOException e) { |
|
|
log.error(e.getMessage(), e); |
|
|
log.error(e.getMessage(), e); |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|