[java 工具集]
自己設計一個 SimpleFileUtil 處理文字檔案
這裡將會介紹一個我自己寫的,而且是經常使用到的一個處理文字檔案的工具集合。該工具主要是封裝檔案的讀取,以及提供一些方便的方法進行對讀取的檔案進行特殊處理,同時也提供方法將一些物件資料輸出到文字檔案中。
主要的功能類
package cn.donespeak.tools.util.file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class SimpleFileUtil {
/**
* 移除本地非目錄檔案
* @param filePath
* @return <code>true</code> 移除成功,
* <code>false</code> 移除失敗
*/
public static boolean removeLocalFile(String filePath){
File f = new File(filePath);
if(f.exists()){
if(f.isDirectory()){
return false ;
}
f.delete();
}
return !f.exists();
}
/**
* 移除一個目錄內所有檔案,同時可以指定是否移除目錄自身
* @param dirPath
* @param removeItself 是否移除目錄自身
* @return 如果removeItself為true, 結果為指定目錄是否已被存在,否則表示指定目錄內的所有檔案是否已經被成功刪除
*/
public static boolean removeDirectory(String dirPath, boolean removeItself){
File dir = new File(dirPath);
if(!dir.exists()){
return true;
}
if(!dir.isDirectory()){
return false;
}
File[] files = dir.listFiles();
for(File f: files){
if(f.isDirectory()){
removeDirectory(f.getAbsolutePath(), true);
} else {
f.delete();
}
}
if(removeItself){
dir.delete();
return !dir.exists();
} else {
files = dir.listFiles();
return files.length == 0;
}
}
/**
* 清空目錄內的所有檔案
* @param dirPath
* @return
*/
public static boolean emptyDirectory(String dirPath){
return removeDirectory(dirPath, false);
}
/**
* 將檔案路徑分隔符替換成系統對應的檔案路徑分隔符
* @param filePath
* @return
*/
public static String normalizeFileSeparator(String filePath){
if(filePath == null){
return null;
}
char targetSeparor = File.separator.charAt(0);
char oldSeparator = '\\';
System.out.println();
System.out.println(targetSeparor == oldSeparator);
if(targetSeparor == oldSeparator){
oldSeparator = '/';
}
return filePath.trim().replace(oldSeparator, targetSeparor);
}
/**
* 將檔案的內容提取為一個字串
* @param filePath
* @return
*/
public static String extractAsOneString(String filePath){
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
String line = null;
while((line = reader.readLine()) != null){
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
/**
* 將檔案中的內容按照提取每行一個字元的方式到一個字串列表
* @param filePath
* @return
* @throws IOException
*/
public static List<String> extractAsOneList(String filePath) throws IOException{
return extractAsOneList(filePath, new StringObjectifier<String>(){
@Override
public String objectify(int index, String line) {
return line;
}
});
}
/**
* 提取檔案內容中的每一個行文字,並利用formater格式化為一個特殊物件
* @param filePath
* @param formater
* @return
* @throws IOException
*/
public static <T> List<T> extractAsOneList(String filePath, StringObjectifier<T> formater) throws IOException{
BufferedReader reader = null;
List<T> list = new ArrayList<T>();
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
String line = null;
int index = 0;
while((line = reader.readLine()) != null){
T t = formater.objectify(index ++, line);
if(t != null){
list.add(t);
}
}
} finally {
if(reader != null) {
try {
reader.close();
reader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}
/**
* 將字串集合按照每個元素一行的方式輸出到一個檔案中
* @param collection
* @param destFilePath
* @param append 是否在原來的檔案中拓展
* @throws IOException
*/
public static void toFile(Collection<String> collection, String destFilePath, boolean append) throws IOException {
toFile(collection, destFilePath, append, new ObjectStringifier<String>(){
@Override
public String stringify(String t) {
return t;
}
});
}
/**
* 將物件集合按照每個元素一行的方式輸出到一個檔案中,
* 可以通過objectStringifier格式化每個物件為一個字串
* @param collection
* @param destFilePath
* @param append 是否在原來的檔案中拓展
* @param objectStringifier 定義物件格式化為字串的方式
* @throws IOException
*/
public static <T> void toFile(Collection<T> collection, String destFilePath, boolean append,
ObjectStringifier<T> objectStringifier) throws IOException{
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFilePath, append)));
for(T t: collection){
String line = objectStringifier.stringify(t);
writer.write(line);
writer.newLine();
}
writer.flush();
} finally {
if(writer != null){
try {
writer.close();
writer = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 拷貝檔案
* @param srcFile
* @param destFile
* @return
*/
public static boolean copyFileTo(File srcFile, File destFile){
if(srcFile == null || !srcFile.exists() || destFile == null){
return false;
}
if(srcFile.getAbsolutePath().equals(destFile.getAbsolutePath())){
return true;
}
try {
if(destFile.exists()){
destFile.delete();
}
Files.copy(srcFile.toPath(), destFile.toPath());
} catch (IOException e) {
return false;
}
return true;
}
public static String getExtension(File file) {
return getExtension(file.getName());
}
public static String getExtension(String filePath) {
int lastPoint = filePath.lastIndexOf(".");
return lastPoint < 0? "": filePath.substring(lastPoint + 1);
}
}
將字串格式化為物件的介面
package cn.donespeak.tools.util.file;
public interface StringObjectifier <T>{
T objectify(int index, String line);
}
將物件格式化為字串的介面
package cn.donespeak.tools.util.file;
public interface ObjectStringifier<T> {
String stringify(T t);
}
org.apache.commons.io.FileUtils
相關推薦
Jodd 一 款優雅的 Java 工具集
BeanUtil 最快的bean處理庫。 一個簡單的JavaBean: 1 2 3 4 5 6 7 8 9 10 11 /** * 拿客 * 網站
[java 工具集]
自己設計一個 SimpleFileUtil 處理文字檔案 這裡將會介紹一個我自己寫的,而且是經常使用到的一個處理文字檔案的工具集合。該工具主要是封裝檔案的讀取,以及提供一些方便的方法進行對讀取的檔案進行特殊處理,同時也提供方法將一些物件資料輸出到文字檔案中。
JAVA-基礎(五) 更多工具集
ron asm 指定 mit 進行 包含 三種 strong token 1.StringTokenizer(字符串標記) StringTokenizer實現枚舉(Enumeration)接口。因此,給定一個輸 入字符串,可以使用StringTokenizer對包含於其中
Java類集框架(六):Stack及Properties子類、Collections工具類
Stack子類 在java.util包內可以利用stack類實現棧的功能。此類定義如下: public class Stack<E> extends Vector<E> Stack類常用方法: 方法 型別
Java基礎(五十九)-集合工具類(Java類集框架)
1:Stack棧 棧是一種先進後出的資料結構。例如:在文字編輯器上都有撤銷功能,那麼每次使用的時候,最後一次的編輯操作永遠是最先撤銷的,那麼這個功能就是利用棧來實現的,棧的基本操作形式如下。 案例:實現棧的操作 import java.util.Stack;
java使用Apache工具集實現ftp檔案傳輸
本文主要介紹如何使用Apache工具集commons-net提供的ftp工具實現向ftp伺服器上傳和下載檔案。 一、準備 需要引用commons-net-3.5.jar包。 使用ma
Java類集 _集合工具類:Collections
掌握Collections 與 Collection 介面的區別 掌握Collections 類中提供的主要操作方法 在面試題目中有可能會問這樣一個問題,請回答, Collections 和 Collection 的關係。 Collections 類與 Collectio
JAVA工具類學習-java 兩個list 交集 並集 差集 去重複並集
List<String> list1 =new ArrayList<String>();list1.add("A");list1.add("B);List<String> list2 =new ArrayList<String>
java工具類,在Windows,Linux系統獲取電腦的MAC地址、本地IP、電腦名
copy iter 去掉m [] equals linu stat cli catch package com.cloudssaas.util; import java.io.BufferedReader; import java.io.IOException;
Java應用集群下的定時任務處理方案(mysql)
運行 1.0 null 都是 bean -a 刷新 bat 任務調度 今天來說一個Java多機部署下定時任務的處理方案。 需求: 有兩臺服務器同時部署了同一套代碼, 代碼中寫有spring自帶的定時任務,但是每次執行定時任務時只需要一臺機器去執行。 當拿到這個需求時我腦子中
binutils工具集之---objdump
objdump content clas 軟件開發 nbsp ont logs span hit 在嵌入式軟件開發中,有時需要知道所生成的程序文件中的段信息以分析問題,或者需要查看c語言對應的匯編代碼,此時,objdump工具就可以幫大忙了。obj——object dum
軟件系統設計工具集
.net googl class hat nbsp 官網 最好的 del 版本 該換換裝備了 今天在看一個模擬器的源碼,一個包裏有多個類,一個類裏又有多個屬性和方法,如果按順序看下來,不僅不能對整個模擬器的框架形成一個大致的認識,而且只會越看越混亂,所以,想到有沒有什麽工
Java類集總結之二
iter return lib 標準 value next() private 叠代 方法 1)Map接口 關系:Map(接口) HashMap(非抽象子類)、TreeMap(非抽象子類) 在開發中,Map集合的內容多用來查詢,全部輸出的操作較少;而Collection接口
JAVA工具包_BeanUtils
自定義 直接 thread back 試用 ng- pad 乒乓球 util 簡介大多數的java開發者通常在創建Java類的時候都會遵循JavaBean的命名模式,對類的屬性生成getters方法和setters方法。通過調用相應的getXxx和setXxx方法,直接訪問
最優化方法與機器學習工具集
ron 區別 分布 .html 高斯 inter 初始 pos pre 摘要: 1.最小二乘法 2.梯度下降法 3.最大(對數)似然估計(MLE) 4.最大後驗估計(MAP) 5.期望最大化算法(EM) 6.牛頓法 7.擬牛頓叠代(BFGS)
java工具包一:日期處理
div 方便 開始 .text simple nor atd blog param 作者:NiceCui 本文謝絕轉載,如需轉載需征得作者本人同意,謝謝。 本文鏈接:http://www.cnblogs.com/NiceCui/p/7846812.html 郵箱:
自定義的jdbc連接工具類JDBCUtils【java 工具類】
tco 成功 val update red source dex imp 添加 JDBCUtils 類: 1. 創建私有的屬性*(連接數據庫必要的四個變量):dreiver url user password 2. 將構造函數私有化 3.將註冊驅動寫入靜態代碼塊
java工具類
new style strings 數組 toarray div ngs tro ron 1.list轉化為數組 ArrayList<String> list=new ArrayList<String>(); String[] strings = n
【轉】角落的開發工具集之Vs(Visual Studio)2017插件推薦
diff image 下載 場景 圖像 部分 bundle emmet down 因為最近錄制視頻的緣故,很多朋友都在QQ群留言,或者微信公眾號私信我,問我一些工具和一些插件啊,怎麽使用的啊?那麽今天我忙裏偷閑整理一下清單,然後在這裏面公布出來。 Visual Stu
實用工具集
table padding bsp ott ace pan adding imp legend 實用工具集 html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,a