/**
* 获取本地文件的数据
*
* @param file
* @return
*/
public static String getLocalDataByFile(File fileName) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
FileInputStream in;
try {
in = new FileInputStream(fileName);
byte buf[] = new byte[in.available()];
in.read(buf);
out.write(buf);
in.close();
out.close();
} catch (IOException e) {
}
return out.toString();
}
/**
* 读取字节文件
*
* @param pathName
* @return
*/
public static byte[] getByteForFile(File inFile) throws IOException{
FileInputStream in= new FileInputStream(inFile);
byte[] buf = new byte[in.available()];
in.read(buf);
in.close();
return buf;
}
/**
* 下载文件到本地路径file中
*
* @param file
* 保存的路径
* @param str
* 要保存的字符串
* @return
*/
public static boolean downFile(File fileName, String str) {
if (!fileName.getParentFile().exists()) {
fileName.getParentFile().mkdirs();
}
boolean res = true;
byte[] content = str.getBytes();
try {
FileOutputStream out = new FileOutputStream(fileName);
out.write(content);
out.close();
} catch (IOException e) {
res = false;
e.printStackTrace();
}
return res;
}
/**
* 删除整个目录
* @param file
* @return
*/
public static void deleteFile(File file){
if(file.exists()){
if(file.listFiles()!=null){
for(File f:file.listFiles()){
f.delete();
}
}
file.delete();
}
}
/**
* 删除目录中的文件
* @param path
*/
public static void deleteDirectory(String path){
File dir=new File(path);
if(dir.exists()){
if(dir.listFiles()!=null){
for(File f:dir.listFiles()){
if(f.isDirectory()){
deleteFile(f);
}else if(f.isFile())
f.delete();
}
}
dir.delete();
}
}
/**
* 对一个文件进行Base64编码
* @param file
* @return
*/
public static String encodeToString(File file){
StringBuffer sb=new StringBuffer();
if(file.exists()){
FileInputStream is=null;
byte[] buffer=new byte[1024];
try {
is=new FileInputStream(file);
int c=0;
while((c=is.read(buffer))!=-1){
sb.append(Base64.encodeToString(buffer, 0, c, Base64.DEFAULT));
}
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**
* 删除图谱文件
*/
public static void deleteEcgFile(File file){
if(file!=null && file.exists()){
File parentFile=file.getParentFile();
file.delete();
if(parentFile!=null && parentFile.listFiles().length <= 0){
parentFile.delete();
}
}
}
/**
* 获取一个目录中最长时间没有被改动的文件
* @param file
*/
public static File findOldFile(File file){
File oldFile = file.listFiles()[0];
long modifiedTime = file.listFiles()[0].lastModified();
for(File f:file.listFiles()){
if(f.lastModified() < modifiedTime){
oldFile=f;
}
modifiedTime=f.lastModified();
}
return oldFile;
}
/**
* 删除一个目录中的所有文件
* @param dir
*/
public static void deleteFileList(File dir){
if(dir.exists()&&dir.listFiles().length>0){
for(File f:dir.listFiles()){
f.delete();
}
}
}
/** 移动文件
* @param src
* @param dest
*/
public static boolean moveFile(String src, String dest) {
File srcFolder = new File(src);
File destFolder = new File(dest);
if(!destFolder.exists())
destFolder.mkdirs();
File newFile = new File(destFolder.getAbsoluteFile() + "/" + srcFolder.getName());
boolean res = srcFolder.renameTo(newFile);
return res;
}
public static boolean saveFile(String text, String path){
File file = new File(path);
if(!file.getParentFile().exists())
file.getParentFile().mkdirs();
FileOutputStream out = null;
boolean result = false;
try {
out = new FileOutputStream(file);
out.write(text.getBytes());
result = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 获取一个文本
* @param inputStream
* @return
*/
public static String readTextFile(InputStream inputStream) {
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(inputStream, "GBK"));
String s ="";
while((s = reader.readLine()) != null){
sb.append(s + "\r\n");
}
inputStream.close();
reader.close();
} catch (IOException e) {
LogHelper.saveExceptionStackInfo(e);
}
return sb.toString();
}
/**
* 截取字节数组
* @param src
* @param begin
* @param count
* @return
*/
public static byte[] subBytes(byte[] src, int begin, int count) {
byte[] bs = new byte[count];
for (int i = begin; i < begin + count; i++)
bs[i - begin] = src[i];
return bs;
}
/**
* 文件转化为字节数组
*
* @param file
* @return
*/
public static byte[] getBytesFromFile(File file) {
byte[] ret = null;
try {
if (file == null) {
return null;
}
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
byte[] b = new byte[4096];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
in.close();
out.close();
ret = out.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}
/**
* 把字节数组保存为一个文件
*
* @param b
* @param outputFile
* @return
*/
public static File getFileFromBytes(byte[] b, String outputFile) {
File ret = null;
BufferedOutputStream stream = null;
try {
ret = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(ret);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return ret;
}