|
|
@@ -1,10 +1,13 @@ |
|
|
|
package cc.smtweb.framework.core.util; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.io.FileUtils; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
import java.nio.charset.Charset; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
|
|
|
/** |
|
|
@@ -14,18 +17,26 @@ import java.nio.charset.StandardCharsets; |
|
|
|
@Slf4j |
|
|
|
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中读取所有内容 |
|
|
|
* |
|
|
|
* @param fn 源文件名 |
|
|
|
* @return byte[] |
|
|
|
*/ |
|
|
|
public static byte[] readFileByte(String fn) { |
|
|
|
File f = new File(fn); |
|
|
|
if (!f.exists()) return null; |
|
|
|
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) { |
|
|
|
log.error(e.getMessage(), e); |
|
|
|
return null; |
|
|
@@ -33,15 +44,15 @@ public class FileUtil { |
|
|
|
} |
|
|
|
|
|
|
|
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 { |
|
|
|
return new String(bytes, encode); |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
return FileUtils.readFileToString(f, encode); |
|
|
|
} catch (IOException e) { |
|
|
|
log.error(e.getMessage(), e); |
|
|
|
return null; |
|
|
|
} |
|
|
|