import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 类说明
*
*/
public class FileUtil {
/**
* 获得文件扩展名(不带句点的)
*
* @param file 文件
* @return
*/
public static String getExtension(File file){
return getExtension(file.getName());
}
/**
* 获得文件扩展名(不带句点的)
*
* @param fileName 文件名,可以含路径
* @return
*/
public static String getExtension(String fileName){
if ((fileName != null) && (fileName.length() > 0)) {
int i = fileName.lastIndexOf('.');
if ((i >-1) && (i < (fileName.length() - 1))) {
return fileName.substring(i + 1);
}
}
return null;
}
/**
* 获得文件名(不带路径的)
*
* @param fileName 文件名,可以含路径
* @return
*/
public static String getFilename(String filePath){
String regExp = ".+\\\\(.+)$";;
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(filePath);
boolean result = m.find();
if(result) return m.group(1);
return filePath;
}
/**
* 复制整个文件夹内容
* @param oldpath String 原文件路径 如:c:/fqf
* @param newpath String 复制后路径 如:f:/fqf/ff
* @param replace String 如果newpath已经存在,是否替换
* @return boolean
* @throws IOException
*/
public static void copyFolder(String oldpath, String newpath,boolean replace) throws IOException {
File f=new File(newpath);
if(f.exists()&&!replace){
return;
}else{
f.mkdirs();
}
File a=new File(oldpath);
String[] file=a.list();
File temp=null;
for (int i = 0; i < file.length; i++) {
if(oldpath.endsWith(File.separator)){
temp=new File(oldpath+file[i]);
}
else{
temp=new File(oldpath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newpath + "/" +
(temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
copyFolder(oldpath+"/"+file[i],newpath+"/"+file[i],replace);
}
}
return;
}
/**
* 获得文件或文件夹的大小
*
* @param path 文件或文件夹路径
* @return
* @throws IOException
*/
public static long getSize(String path) throws IOException {
long size=0;
File f=new File(path);
if(f.isFile()){
size=f.length();
}else{
String[] file=f.list();
File temp=null;
for (int i = 0; i < file.length; i++) {
if(path.endsWith(File.separator)){
temp=new File(path+file[i]);
}
else{
temp=new File(path+File.separator+file[i]);
}
size+=getSize(temp.getPath());
}
}
return size;
}
/**
* 删除文件
* @param filepathandname String 文件路径及名称 如c:/fqf.txt
* @param filecontent String
* @return boolean
*/
public static void deleteFile(String filepathandname) {
File mydelfile = new File(filepathandname);
if(mydelfile.exists()){
mydelfile.delete();
}
}
/**
* 删除文件夹
* @param folderpath String 文件夹路径及名称 如c:/fqf
* @param filecontent String
* @return boolean
*/
public static void deleteFolder(String folderpath) {
deleteAllOfFolder(folderpath); //删除完里面所有内容
File myfilepath = new File(folderpath);
myfilepath.delete(); //删除空文件夹
}
/**
* 删除文件夹里面的所有文件
* @param path String 文件夹路径 如 c:/fqf
*/
public static void deleteAllOfFolder(String path) {
File file = new File(path);
if (!file.exists() || !file.isDirectory()) {
return;
}
String[] templist = file.list();
File temp = null;
for (int i = 0; i < templist.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + templist[i]);
}else {
temp = new File(path + File.separator + templist[i]);
}
if (temp.isFile()) {
temp.delete();
}else if (temp.isDirectory()) {
deleteFolder(path+"/"+ templist[i]);//删除空文件夹
}
}
}
/**
* 复制单个文件
* @param oldpath String 原文件路径 如:c:/fqf.txt
* @param newpath String 复制后路径 如:f:/fqf.txt
* @throws IOException
*/
public static void copyFile(String oldpath, String newpath) throws IOException {
//int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldpath);
if (oldfile.exists()) { //文件存在时
InputStream is = new FileInputStream(oldpath); //读入原文件
FileOutputStream os = new FileOutputStream(newpath);
byte[] buffer = new byte[1444];
while ( (byteread = is.read(buffer)) != -1) {
//bytesum += byteread; //字节数 文件大小
//System.out.println(bytesum);
os.write(buffer, 0, byteread);
}
os.close();
is.close();
}
}
/**
* 移动文件到指定目录
* @param oldpath String 如:c:/fqf.txt
* @param newpath String 如:d:/fqf.txt
* @throws IOException
*/
public static void moveFile(String oldpath, String newpath) throws IOException {
copyFile(oldpath, newpath);
deleteFile(oldpath);
}
/**
* 移动文件到指定目录
* @param oldpath String 如:c:/fqf.txt
* @param newpath String 如:d:/fqf.txt
* @throws IOException
*/
public static void movefolder(String oldpath, String newpath) throws IOException {
copyFolder(oldpath, newpath, true);
deleteFolder(oldpath);
}
/**
* 获得文件内容
*
* @param fileName 文件绝对路径
* @return
* @throws IOException
*/
public static String getContent(String fileName) throws IOException{
StringBuffer strBuf = new StringBuffer();
FileReader fr=null;
BufferedReader br=null;
File file = new File(fileName);
if (file.exists()) {
int readNum=0;
fr = new FileReader(fileName);
br = new BufferedReader(fr);
char[] charBuffer=new char[1024];
while ( (readNum=br.read(charBuffer)) != -1) {
strBuf.append(charBuffer,0,readNum);
}
br.close();
fr.close();
return strBuf.toString();
}else{
return null;
}
}
/**
* 获得文件内容
*
* @param fileName 文件绝对路径
* @param encoding 文件编码
* @return
* @throws IOException
*/
public static String getContent(String fileName,String encoding) throws IOException{
StringBuffer strBuf = new StringBuffer();
InputStreamReader br=null;
File file = new File(fileName);
if (file.exists()) {
int readNum=0;
br = new InputStreamReader(new FileInputStream(file),encoding);
char[] charBuffer=new char[1024];
while ( (readNum=br.read(charBuffer)) != -1) {
strBuf.append(charBuffer,0,readNum);
}
br.close();
return strBuf.toString();
}else{
return null;
}
}
/**
* 建立文本文件
*
* @param fileName 文件绝对路径
* @param content 文件初始化内容
* @param replace 文件存在时是否覆盖
* @return
* @throws IOException
*/
public static void createTextFile(String fileName, String content, boolean replace) throws IOException{
File file = new File(fileName);
if (file.exists() && !replace) return;
FileWriter fw = null ;
BufferedWriter bw = null;
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
bw.close();
fw.close();
}
/**
* 建立文本文件
*
* @param fileName 文件绝对路径
* @param content 文件初始化内容
* @param replace 文件存在时是否覆盖
* @param encoding 输入时编码
* @return
* @throws IOException
*/
public static void createTextFile(String fileName, String content, boolean replace, String encoding) throws IOException{
File file = new File(fileName);
if (file.exists() && !replace) return;
// FileWriter fw = null ;
BufferedWriter bw = null;
// fw = new FileWriter(fileName);
// FileOutputStream s=null;
OutputStreamWriter s=new OutputStreamWriter(new FileOutputStream(file),encoding);
bw = new BufferedWriter(s);
bw.write(content);
bw.flush();
bw.close();
s.close();
//fw.close();
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 类说明
*
*/
public class FileUtil {
/**
* 获得文件扩展名(不带句点的)
*
* @param file 文件
* @return
*/
public static String getExtension(File file){
return getExtension(file.getName());
}
/**
* 获得文件扩展名(不带句点的)
*
* @param fileName 文件名,可以含路径
* @return
*/
public static String getExtension(String fileName){
if ((fileName != null) && (fileName.length() > 0)) {
int i = fileName.lastIndexOf('.');
if ((i >-1) && (i < (fileName.length() - 1))) {
return fileName.substring(i + 1);
}
}
return null;
}
/**
* 获得文件名(不带路径的)
*
* @param fileName 文件名,可以含路径
* @return
*/
public static String getFilename(String filePath){
String regExp = ".+\\\\(.+)$";;
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(filePath);
boolean result = m.find();
if(result) return m.group(1);
return filePath;
}
/**
* 复制整个文件夹内容
* @param oldpath String 原文件路径 如:c:/fqf
* @param newpath String 复制后路径 如:f:/fqf/ff
* @param replace String 如果newpath已经存在,是否替换
* @return boolean
* @throws IOException
*/
public static void copyFolder(String oldpath, String newpath,boolean replace) throws IOException {
File f=new File(newpath);
if(f.exists()&&!replace){
return;
}else{
f.mkdirs();
}
File a=new File(oldpath);
String[] file=a.list();
File temp=null;
for (int i = 0; i < file.length; i++) {
if(oldpath.endsWith(File.separator)){
temp=new File(oldpath+file[i]);
}
else{
temp=new File(oldpath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newpath + "/" +
(temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
copyFolder(oldpath+"/"+file[i],newpath+"/"+file[i],replace);
}
}
return;
}
/**
* 获得文件或文件夹的大小
*
* @param path 文件或文件夹路径
* @return
* @throws IOException
*/
public static long getSize(String path) throws IOException {
long size=0;
File f=new File(path);
if(f.isFile()){
size=f.length();
}else{
String[] file=f.list();
File temp=null;
for (int i = 0; i < file.length; i++) {
if(path.endsWith(File.separator)){
temp=new File(path+file[i]);
}
else{
temp=new File(path+File.separator+file[i]);
}
size+=getSize(temp.getPath());
}
}
return size;
}
/**
* 删除文件
* @param filepathandname String 文件路径及名称 如c:/fqf.txt
* @param filecontent String
* @return boolean
*/
public static void deleteFile(String filepathandname) {
File mydelfile = new File(filepathandname);
if(mydelfile.exists()){
mydelfile.delete();
}
}
/**
* 删除文件夹
* @param folderpath String 文件夹路径及名称 如c:/fqf
* @param filecontent String
* @return boolean
*/
public static void deleteFolder(String folderpath) {
deleteAllOfFolder(folderpath); //删除完里面所有内容
File myfilepath = new File(folderpath);
myfilepath.delete(); //删除空文件夹
}
/**
* 删除文件夹里面的所有文件
* @param path String 文件夹路径 如 c:/fqf
*/
public static void deleteAllOfFolder(String path) {
File file = new File(path);
if (!file.exists() || !file.isDirectory()) {
return;
}
String[] templist = file.list();
File temp = null;
for (int i = 0; i < templist.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + templist[i]);
}else {
temp = new File(path + File.separator + templist[i]);
}
if (temp.isFile()) {
temp.delete();
}else if (temp.isDirectory()) {
deleteFolder(path+"/"+ templist[i]);//删除空文件夹
}
}
}
/**
* 复制单个文件
* @param oldpath String 原文件路径 如:c:/fqf.txt
* @param newpath String 复制后路径 如:f:/fqf.txt
* @throws IOException
*/
public static void copyFile(String oldpath, String newpath) throws IOException {
//int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldpath);
if (oldfile.exists()) { //文件存在时
InputStream is = new FileInputStream(oldpath); //读入原文件
FileOutputStream os = new FileOutputStream(newpath);
byte[] buffer = new byte[1444];
while ( (byteread = is.read(buffer)) != -1) {
//bytesum += byteread; //字节数 文件大小
//System.out.println(bytesum);
os.write(buffer, 0, byteread);
}
os.close();
is.close();
}
}
/**
* 移动文件到指定目录
* @param oldpath String 如:c:/fqf.txt
* @param newpath String 如:d:/fqf.txt
* @throws IOException
*/
public static void moveFile(String oldpath, String newpath) throws IOException {
copyFile(oldpath, newpath);
deleteFile(oldpath);
}
/**
* 移动文件到指定目录
* @param oldpath String 如:c:/fqf.txt
* @param newpath String 如:d:/fqf.txt
* @throws IOException
*/
public static void movefolder(String oldpath, String newpath) throws IOException {
copyFolder(oldpath, newpath, true);
deleteFolder(oldpath);
}
/**
* 获得文件内容
*
* @param fileName 文件绝对路径
* @return
* @throws IOException
*/
public static String getContent(String fileName) throws IOException{
StringBuffer strBuf = new StringBuffer();
FileReader fr=null;
BufferedReader br=null;
File file = new File(fileName);
if (file.exists()) {
int readNum=0;
fr = new FileReader(fileName);
br = new BufferedReader(fr);
char[] charBuffer=new char[1024];
while ( (readNum=br.read(charBuffer)) != -1) {
strBuf.append(charBuffer,0,readNum);
}
br.close();
fr.close();
return strBuf.toString();
}else{
return null;
}
}
/**
* 获得文件内容
*
* @param fileName 文件绝对路径
* @param encoding 文件编码
* @return
* @throws IOException
*/
public static String getContent(String fileName,String encoding) throws IOException{
StringBuffer strBuf = new StringBuffer();
InputStreamReader br=null;
File file = new File(fileName);
if (file.exists()) {
int readNum=0;
br = new InputStreamReader(new FileInputStream(file),encoding);
char[] charBuffer=new char[1024];
while ( (readNum=br.read(charBuffer)) != -1) {
strBuf.append(charBuffer,0,readNum);
}
br.close();
return strBuf.toString();
}else{
return null;
}
}
/**
* 建立文本文件
*
* @param fileName 文件绝对路径
* @param content 文件初始化内容
* @param replace 文件存在时是否覆盖
* @return
* @throws IOException
*/
public static void createTextFile(String fileName, String content, boolean replace) throws IOException{
File file = new File(fileName);
if (file.exists() && !replace) return;
FileWriter fw = null ;
BufferedWriter bw = null;
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
bw.close();
fw.close();
}
/**
* 建立文本文件
*
* @param fileName 文件绝对路径
* @param content 文件初始化内容
* @param replace 文件存在时是否覆盖
* @param encoding 输入时编码
* @return
* @throws IOException
*/
public static void createTextFile(String fileName, String content, boolean replace, String encoding) throws IOException{
File file = new File(fileName);
if (file.exists() && !replace) return;
// FileWriter fw = null ;
BufferedWriter bw = null;
// fw = new FileWriter(fileName);
// FileOutputStream s=null;
OutputStreamWriter s=new OutputStreamWriter(new FileOutputStream(file),encoding);
bw = new BufferedWriter(s);
bw.write(content);
bw.flush();
bw.close();
s.close();
//fw.close();
}
}