Java文件操作工具類(lèi)fileUtil實(shí)例【文件增刪改,復(fù)制等】
本文實(shí)例講述了Java文件操作工具類(lèi)fileUtil。分享給大家供大家參考,具體如下:
package com.gcloud.common;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
/**
* 文件工具類(lèi)
* Created by charlin on 2017/9/8.
*/
public class FileUtil {
/**
* 讀取文件內(nèi)容
*
* @param is
* @return
*/
public static String readFile(InputStream is) {
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try {
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String readLine = null;
while ((readLine = br.readLine()) != null) {
sb.append(readLine);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**
* 判斷指定的文件是否存在。
*
* @param fileName
* @return
*/
public static boolean isFileExist(String fileName) {
return new File(fileName).isFile();
}
/**
* 創(chuàng)建指定的目錄。 如果指定的目錄的父目錄不存在則創(chuàng)建其目錄書(shū)上所有需要的父目錄。
* 注意:可能會(huì)在返回false的時(shí)候創(chuàng)建部分父目錄。
*
* @param file
* @return
*/
public static boolean makeDirectory(File file) {
File parent = file.getParentFile();
if (parent != null) {
return parent.mkdirs();
}
return false;
}
/**
* 返回文件的URL地址。
*
* @param file
* @return
* @throws MalformedURLException
*/
public static URL getURL(File file) throws MalformedURLException {
String fileURL = "file:/" + file.getAbsolutePath();
URL url = new URL(fileURL);
return url;
}
/**
* 從文件路徑得到文件名。
*
* @param filePath
* @return
*/
public static String getFileName(String filePath) {
File file = new File(filePath);
return file.getName();
}
/**
* 從文件名得到文件絕對(duì)路徑。
*
* @param fileName
* @return
*/
public static String getFilePath(String fileName) {
File file = new File(fileName);
return file.getAbsolutePath();
}
/**
* 將DOS/Windows格式的路徑轉(zhuǎn)換為UNIX/Linux格式的路徑。
*
* @param filePath
* @return
*/
public static String toUNIXpath(String filePath) {
return filePath.replace("", "/");
}
/**
* 從文件名得到UNIX風(fēng)格的文件絕對(duì)路徑。
*
* @param fileName
* @return
*/
public static String getUNIXfilePath(String fileName) {
File file = new File(fileName);
return toUNIXpath(file.getAbsolutePath());
}
/**
* 得到文件后綴名
*
* @param fileName
* @return
*/
public static String getFileExt(String fileName) {
int point = fileName.lastIndexOf('.');
int length = fileName.length();
if (point == -1 || point == length - 1) {
return "";
} else {
return fileName.substring(point + 1, length);
}
}
/**
* 得到文件的名字部分。 實(shí)際上就是路徑中的最后一個(gè)路徑分隔符后的部分。
*
* @param fileName
* @return
*/
public static String getNamePart(String fileName) {
int point = getPathLastIndex(fileName);
int length = fileName.length();
if (point == -1) {
return fileName;
} else if (point == length - 1) {
int secondPoint = getPathLastIndex(fileName, point - 1);
if (secondPoint == -1) {
if (length == 1) {
return fileName;
} else {
return fileName.substring(0, point);
}
} else {
return fileName.substring(secondPoint + 1, point);
}
} else {
return fileName.substring(point + 1);
}
}
/**
* 得到文件名中的父路徑部分。 對(duì)兩種路徑分隔符都有效。 不存在時(shí)返回""。
* 如果文件名是以路徑分隔符結(jié)尾的則不考慮該分隔符,例如"/path/"返回""。
*
* @param fileName
* @return
*/
public static String getPathPart(String fileName) {
int point = getPathLastIndex(fileName);
int length = fileName.length();
if (point == -1) {
return "";
} else if (point == length - 1) {
int secondPoint = getPathLastIndex(fileName, point - 1);
if (secondPoint == -1) {
return "";
} else {
return fileName.substring(0, secondPoint);
}
} else {
return fileName.substring(0, point);
}
}
/**
* 得到路徑分隔符在文件路徑中最后出現(xiàn)的位置。 對(duì)于DOS或者UNIX風(fēng)格的分隔符都可以。
*
* @param fileName
* @return
*/
public static int getPathLastIndex(String fileName) {
int point = fileName.lastIndexOf("/");
if (point == -1) {
point = fileName.lastIndexOf("");
}
return point;
}
/**
* 得到路徑分隔符在文件路徑中指定位置前最后出現(xiàn)的位置。 對(duì)于DOS或者UNIX風(fēng)格的分隔符都可以。
*
* @param fileName
* @param fromIndex
* @return
*/
public static int getPathLastIndex(String fileName, int fromIndex) {
int point = fileName.lastIndexOf("/", fromIndex);
if (point == -1) {
point = fileName.lastIndexOf("", fromIndex);
}
return point;
}
/**
* 得到路徑分隔符在文件路徑中首次出現(xiàn)的位置。 對(duì)于DOS或者UNIX風(fēng)格的分隔符都可以。
*
* @param fileName
* @return
*/
public static int getPathIndex(String fileName) {
int point = fileName.indexOf("/");
if (point == -1) {
point = fileName.indexOf("");
}
return point;
}
/**
* 得到路徑分隔符在文件路徑中指定位置后首次出現(xiàn)的位置。 對(duì)于DOS或者UNIX風(fēng)格的分隔符都可以。
*
* @param fileName
* @param fromIndex
* @return
*/
public static int getPathIndex(String fileName, int fromIndex) {
int point = fileName.indexOf("/", fromIndex);
if (point == -1) {
point = fileName.indexOf("", fromIndex);
}
return point;
}
/**
* 將文件名中的類(lèi)型部分去掉。
*
* @param filename
* @return
*/
public static String removeFileExt(String filename) {
int index = filename.lastIndexOf(".");
if (index != -1) {
return filename.substring(0, index);
} else {
return filename;
}
}
/**
* 得到相對(duì)路徑。 文件名不是目錄名的子節(jié)點(diǎn)時(shí)返回文件名。
*
* @param pathName
* @param fileName
* @return
*/
public static String getSubpath(String pathName, String fileName) {
int index = fileName.indexOf(pathName);
if (index != -1) {
return fileName.substring(index + pathName.length() + 1);
} else {
return fileName;
}
}
/**
* 刪除一個(gè)文件。
*
* @param filename
* @throws IOException
*/
public static void deleteFile(String filename) throws IOException {
File file = new File(filename);
if (file.isDirectory()) {
throw new IOException("IOException -> BadInputException: not a file.");
}
if (!file.exists()) {
throw new IOException("IOException -> BadInputException: file is not exist.");
}
if (!file.delete()) {
throw new IOException("Cannot delete file. filename = " + filename);
}
}
/**
* 刪除文件夾及其下面的子文件夾
*
* @param dir
* @throws IOException
*/
public static void deleteDir(File dir) throws IOException {
if (dir.isFile())
throw new IOException("IOException -> BadInputException: not a directory.");
File[] files = dir.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isFile()) {
file.delete();
} else {
deleteDir(file);
}
}
}
dir.delete();
}
/**
* 復(fù)制文件
*
* @param src
* @param dst
* @throws Exception
*/
public static void copy(File src, File dst) throws Exception {
int BUFFER_SIZE = 4096;
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
throw e;
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
in = null;
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
out = null;
}
}
}
/**
* @復(fù)制文件,支持把源文件內(nèi)容追加到目標(biāo)文件末尾
* @param src
* @param dst
* @param append
* @throws Exception
*/
public static void copy(File src, File dst, boolean append) throws Exception {
int BUFFER_SIZE = 4096;
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst, append), BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
throw e;
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
in = null;
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
out = null;
}
}
}
}
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
MyBatis批量插入/修改/刪除MySql數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于MyBatis批量插入/修改/刪除MySql數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Java 7菱形語(yǔ)法與泛型構(gòu)造器實(shí)例分析
這篇文章主要介紹了Java 7菱形語(yǔ)法與泛型構(gòu)造器,結(jié)合實(shí)例形式分析了Java菱形語(yǔ)法與泛型構(gòu)造器相關(guān)原理與使用技巧,需要的朋友可以參考下2019-07-07
Activiti如何啟動(dòng)流程并使流程前進(jìn)
這篇文章主要介紹了Activiti如何啟動(dòng)流程并使流程前進(jìn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Spring依賴(lài)注入方式(Dependency Injection)
在實(shí)際開(kāi)發(fā)中,推薦使用構(gòu)造器注入而非字段注入,原因包括官方推薦、代碼簡(jiǎn)潔、安全性高和易于測(cè)試,構(gòu)造器注入通過(guò)在對(duì)象創(chuàng)建時(shí)注入所有必需的依賴(lài),避免運(yùn)行時(shí)忘記注入的問(wèn)題,且利用Lombok的@RequiredArgsConstructor可自動(dòng)生成構(gòu)造函數(shù)2024-10-10
mybatis一級(jí)緩存和二級(jí)緩存的區(qū)別及說(shuō)明
Spring boot整合Springfox生成restful的在線api文檔

