|
|
@ -305,23 +305,22 @@ public class FileUtil { |
|
|
* @return |
|
|
* @return |
|
|
*/ |
|
|
*/ |
|
|
public static byte[] file2byte(String filePath) { |
|
|
public static byte[] file2byte(String filePath) { |
|
|
byte[] buffer = null; |
|
|
|
|
|
try { |
|
|
|
|
|
File file = new File(filePath); |
|
|
|
|
|
FileInputStream fis = new FileInputStream(file); |
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
|
|
|
|
|
byte[] b = new byte[1024]; |
|
|
|
|
|
|
|
|
File file = new File(filePath); |
|
|
|
|
|
if (!file.exists() || !file.isFile()) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
try (FileInputStream fis = new FileInputStream(file); |
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length())) { |
|
|
|
|
|
byte[] buffer = new byte[8192]; // 8KB 更合理
|
|
|
int n; |
|
|
int n; |
|
|
while ((n = fis.read(b)) != -1) { |
|
|
|
|
|
bos.write(b, 0, n); |
|
|
|
|
|
|
|
|
while ((n = fis.read(buffer)) != -1) { |
|
|
|
|
|
bos.write(buffer, 0, n); |
|
|
} |
|
|
} |
|
|
fis.close(); |
|
|
|
|
|
bos.close(); |
|
|
|
|
|
buffer = bos.toByteArray(); |
|
|
|
|
|
} catch (Throwable t) { |
|
|
|
|
|
LoggerUtil.e("file2byte", StringUtil.getThrowableStr(t)); |
|
|
|
|
|
|
|
|
return bos.toByteArray(); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
LoggerUtil.e("file2byte", StringUtil.getThrowableStr(e)); |
|
|
|
|
|
return null; |
|
|
} |
|
|
} |
|
|
return buffer; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -413,7 +412,6 @@ public class FileUtil { |
|
|
if (!file.exists()) file.createNewFile(); |
|
|
if (!file.exists()) file.createNewFile(); |
|
|
FileWriter fileWriter = new FileWriter(filePath, append); |
|
|
FileWriter fileWriter = new FileWriter(filePath, append); |
|
|
fileWriter.write(content); |
|
|
fileWriter.write(content); |
|
|
if (fileWriter == null) return; |
|
|
|
|
|
fileWriter.flush(); |
|
|
fileWriter.flush(); |
|
|
fileWriter.close(); |
|
|
fileWriter.close(); |
|
|
} catch (Throwable t) { |
|
|
} catch (Throwable t) { |
|
|
@ -579,21 +577,24 @@ public class FileUtil { |
|
|
/** |
|
|
/** |
|
|
* 读取本地JSON,显示汉字正确,txt文件设置时UTF-8,UNIX |
|
|
* 读取本地JSON,显示汉字正确,txt文件设置时UTF-8,UNIX |
|
|
* */ |
|
|
* */ |
|
|
public static String readLocalJsonForStorage(Context context, String fileName){ |
|
|
|
|
|
|
|
|
|
|
|
String resultString=""; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
File file = new File(fileName); |
|
|
|
|
|
InputStream inputStream=new FileInputStream(file); |
|
|
|
|
|
byte[] buffer=new byte[inputStream.available()]; |
|
|
|
|
|
inputStream.read(buffer); |
|
|
|
|
|
resultString=new String(buffer,"GB2312"); |
|
|
|
|
|
inputStream.close(); |
|
|
|
|
|
|
|
|
public static String readLocalJsonForStorage(Context context, String fileName) { |
|
|
|
|
|
File file = new File(fileName); |
|
|
|
|
|
if (!file.exists()) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
|
try (InputStream is = new FileInputStream(file); |
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(is, "UTF-8"); |
|
|
|
|
|
BufferedReader br = new BufferedReader(isr)) { |
|
|
|
|
|
String line; |
|
|
|
|
|
while ((line = br.readLine()) != null) { |
|
|
|
|
|
sb.append(line); |
|
|
|
|
|
} |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
// TODO: handle exception
|
|
|
|
|
|
|
|
|
e.printStackTrace(); // 至少打日志
|
|
|
|
|
|
return null; |
|
|
} |
|
|
} |
|
|
return resultString; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sb.toString(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|