Commit 33051cde authored by 张永's avatar 张永

加一个

parent a80a68f1
......@@ -14,9 +14,13 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
......@@ -89,7 +93,7 @@ public class FileUtil {
public final static String getExtName(File file) {
String extName = null;
String fileName = file.getName();
if(fileName.contains(".")) {
if (fileName.contains(".")) {
extName = fileName.substring(fileName.lastIndexOf(".") + 1).toUpperCase();
}
return extName;
......@@ -126,7 +130,7 @@ public class FileUtil {
InputStream is = null;
try {
String extName = getExtName(file);
if(StringUtil.isNotBlank(extName)) {
if (StringUtil.isNotBlank(extName)) {
return extName;
} else {
is = new FileInputStream(file);
......@@ -137,7 +141,7 @@ public class FileUtil {
return getExtName(file);
} finally {
try {
if(is != null) {
if (is != null) {
is.close();
}
} catch (IOException e) {
......@@ -196,13 +200,14 @@ public class FileUtil {
/**
* 将字节数组转化为文件
*
* @param bytes
* @param filename
* @return
*/
public static File bytesToFile(byte[] bytes, File file) {
File parent = file.getParentFile();
if(!parent.exists()) {
if (!parent.exists()) {
parent.mkdirs();
}
BufferedOutputStream stream = null;
......@@ -230,13 +235,14 @@ public class FileUtil {
/**
* 将字节数组转化为文件
*
* @param bytes
* @param filenames
* @return
*/
public static File[] bytesToFiles(byte[][] bytes, String[] filenames) {
File[] files = new File[filenames.length];
for(int i = 0; i < filenames.length; i++) {
for (int i = 0; i < filenames.length; i++) {
files[i] = bytesToFile(bytes[i], filenames[i]);
}
return files;
......@@ -244,6 +250,7 @@ public class FileUtil {
/**
* 将文件转化为字节数组
*
* @param file
* @return
*/
......@@ -268,12 +275,13 @@ public class FileUtil {
/**
* 将文件转化为字节数组
*
* @param files
* @return
*/
public static byte[][] filesToBytes(File[] files) {
byte[][] datas = new byte[files.length][];
for(int i = 0; i < files.length; i++) {
for (int i = 0; i < files.length; i++) {
datas[i] = fileToBytes(files[i]);
}
return datas;
......@@ -322,17 +330,18 @@ public class FileUtil {
/**
* 移动文件
*
* @param src 原文件
* @param des 目标文件
* @throws IOException
*/
public static void fileMove(File src, File des) {
if(src != null) {
if (src != null) {
File parent = des.getParentFile();
if(!parent.exists()) {
if (!parent.exists()) {
parent.mkdirs();
}
if(!src.renameTo(des)) {
if (!src.renameTo(des)) {
fileCopy(src, des);
fileDelete(src);
}
......@@ -343,10 +352,11 @@ public class FileUtil {
/**
* 删除文件
*
* @param file 目标文件
*/
public static void fileDelete(File... files) {
for(File file : files) {
for (File file : files) {
if (file.exists()) {
if (file.isFile()) {
file.delete();
......@@ -398,13 +408,13 @@ public class FileUtil {
OutputStreamWriter osw = null;
Writer writer = null;
try {
if(!file.getParentFile().exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos, "UTF-8");
writer = new BufferedWriter(osw);
for(String string : strings) {
for (String string : strings) {
writer.append(string).append(System.getProperty("line.separator"));
}
} catch (IOException e) {
......@@ -455,10 +465,10 @@ public class FileUtil {
public static File createFile(File file) {
File folder = file.getParentFile();
if(!(folder.exists() && folder.isDirectory())) {
if (!(folder.exists() && folder.isDirectory())) {
folder.mkdirs();
}
if(!file.exists() || file.isDirectory()) {
if (!file.exists() || file.isDirectory()) {
try {
file.createNewFile();
} catch (IOException e) {
......@@ -472,7 +482,7 @@ public class FileUtil {
public static File createFolder(String name) {
File folder = new File(name);
if(!folder.exists() || folder.isFile()) {
if (!folder.exists() || folder.isFile()) {
folder.mkdirs();
}
return folder;
......@@ -552,7 +562,6 @@ public class FileUtil {
return !new File(jspname).exists();
}
public static Object fileToObject(File file) {
FileInputStream fis = null;
ObjectInputStream ois = null;
......@@ -576,11 +585,59 @@ public class FileUtil {
}
}
public static void urlToFile(String url, String savePath,String fileName) {
InputStream is = null;
OutputStream os = null;
try {
// 构造URL
URL weburl = new URL(url);
// 打开连接
URLConnection con = weburl.openConnection();
// 设置请求超时为5s
con.setConnectTimeout(5 * 1000);
// 输入流
is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
File sf = new File(savePath);
if (!sf.exists()) {
sf.mkdirs();
}
os = new FileOutputStream(savePath+"/"+fileName);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void appendMethod(String fileName, String content) {
try {
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content+"\n");
writer.write(content + "\n");
writer.close();
} catch (IOException e) {
e.printStackTrace();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment