You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.7 KiB

3 years ago
package com.eco.plugin.xx.cjccfilter.utils;
import java.io.*;
public class IOUtils {
/**
* 将inputStream写入本地文件
* @param url
* @param inputStream
*/
public static void writeFile(String url, InputStream inputStream){
int index;
byte[] bytes = new byte[1024];
FileOutputStream downloadFile = null ;
try {
downloadFile = new FileOutputStream(url);
while ((index = inputStream.read(bytes)) != -1) {
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
}catch(Exception e){
FRUtils.FRLogInfo("Exception:writeFile:"+e.getMessage());
}finally {
try {
inputStream.close();
downloadFile.close();
}
catch (Exception e){
FRUtils.FRLogInfo("Exception:writeFile:finally"+e.getMessage());
}
}
}
/**
* 字符串转输入流
* @param str
* @return
*/
public static InputStream strToInputStream(String str){
try {
return new ByteArrayInputStream(str.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
FRUtils.FRLogInfo("IOUtils-strToInputStream:exception:"+e.getMessage());
}
return null;
}
public static String inputStreamToStr(InputStream inputStream) {
StringBuffer stringBuffer = new StringBuffer();
byte[] byt = new byte[4096];
try {
for (int i; (i = inputStream.read(byt)) != -1; ) {
stringBuffer.append(new String(byt, 0, i));
}
}catch(Exception e){
FRUtils.FRLogInfo("Exception:inputStreamToStr:"+e.getMessage());
}
return stringBuffer.toString();
}
/**
* 读取txt文件
* @param file
* @return
*/
public static String readTxtFile(File file){
if(file == null || !file.isFile() || !file.exists()){
return "";
}
String result = "";
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(file));
String line = "";
while((line = br.readLine()) != null){
result += "\n"+line;
}
br.close();
}catch(Exception e){
FRUtils.FRLogError("读取txt文件异常!");
}finally {
if(br != null){
try {
br.close();
} catch (IOException e) {
FRUtils.FRLogError("BufferedReader关闭异常");
}
}
}
return result;
}
}