1、FileUtil

Former-commit-id: fd7dfe188a5165d44612f1bfa8073418be81679f
master
wangbing 5 years ago
parent 7e25c513e4
commit da5c4cd996

@ -1,9 +1,48 @@
package ${basePackage}.frame.utils;
import java.io.*;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class FileUtil {
public static List<String> readFile2Lines(String fileName) {
List<String> returnString = new ArrayList<String>();
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
returnString.add(tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return returnString;
}
public static String readFileToString(File file) throws IOException {
byte[] bytes = readFileToByteArray(file);
return new String(bytes, "UTF-8");
@ -116,6 +155,18 @@ public class FileUtil {
return count > 2147483647L ? -1 : (int) count;
}
public static boolean copy(File in, File out) throws Exception {
try {
FileInputStream input = new FileInputStream(in);
FileOutputStream output = new FileOutputStream(out);
copy(input, output);
input.close();
output.close();
return true;
} catch (IOException ie) {
return false;
}
}
public static long copyLarge(InputStream input, OutputStream output) throws IOException {
return copy(input, output, 4096);
@ -134,4 +185,58 @@ public class FileUtil {
return count;
}
public static boolean clearDirectory(File dir) {
if ((dir == null) || !dir.isDirectory()) {
throw new IllegalArgumentException("Argument " + dir +
" is not a directory. ");
}
File[] entries = dir.listFiles();
int sz = entries.length;
for (int i = 0; i < sz; i++) {
if (entries[i].isDirectory()) {
if (!clearDirectory(entries[i])) {
return false;
}
} else {
if (!entries[i].delete()) {
return false;
}
}
}
if (!dir.delete()) {
return false;
}
return true;
}
/**
* DOS/WindowsUNIX/Linux
* "\"全部换为"/"便
* "/""\"DOS/Windows
*/
public static String toUNIXpath(String filePath) {
return filePath.replace('\\', '/');
}
public static void inputStream2File(InputStream is, File target) {
try {
if (!target.exists() && !target.createNewFile()) {
throw new RuntimeException(target.getName() + "not exists");
}
OutputStream outStream = new FileOutputStream(target);
byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Loading…
Cancel
Save

Powered by TurnKey Linux.