生活随笔
收集整理的這篇文章主要介紹了
Java IO流学习总结七:Commons IO 2.5-FileUtils
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java IO流學習總結七:Commons IO 2.5-FileUtils
轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/54972773
本文出自【趙彥軍的博客】
Java IO流學習總結一:輸入輸出流
Java IO流學習總結二:File
Java IO流學習總結三:緩沖流-BufferedInputStream、BufferedOutputStream
Java IO流學習總結四:緩沖流-BufferedReader、BufferedWriter
Java IO流學習總結五:轉換流-InputStreamReader、OutputStreamWriter
Java IO流學習總結六:ByteArrayInputStream、ByteArrayOutputStream
Java IO流學習總結七:Commons IO 2.5-FileUtils
2021年 Java Okio-更加高效易用的IO庫
在上面的幾篇文章中,介紹了IO的常規用法,今天介紹Commons IO框架的使用。
Commons IO簡介
Apache Commons IO是Apache基金會創建并維護的Java函數庫。它提供了許多類使得開發者的常見任務變得簡單,同時減少重復代碼,這些代碼可能遍布于每個獨立的項目中,你卻不得不重復的編寫。這些類由經驗豐富的開發者維護,對各種問題的邊界條件考慮周到,并持續修復相關bug。最新版本2.5
下載地址:http://commons.apache.org/proper/commons-io/download_io.cgi
福利
另外我已經把Commons IO 2.5的源碼發布到Jcenter,大家就不用下載jar包了,可以直接引用。
<dependency><groupId>org
.apache
.commons
.io
</groupId
><artifactId>commonsIO
</artifactId
><version>2.5.0</version
><type>pom
</type
>
</dependency
>
compile
'org.apache.commons.io:commonsIO:2.5.0'
FileUtils 文件操作工具類
void copyDirectory( file1
, file2
);
void copyDirectoryToDirectory( file1
, file2
);
void copyDirectory(File srcDir
, File destDir
, FileFilter filter
)
void copyFile(final File srcFile
, final File destFile
) void long copyFile(final File input
, final OutputStream output
) void copyFileToDirectory( file1
, file2
)
void copyInputStreamToFile( InputStream source
, File destination
)
void copyURLToFile(final URL source
, final File destination
)
void copyURLToFile(final URL source
, final File destination
,final int connectionTimeout
, final int readTimeout
)
實戰演練
package com.app;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;import org.apache.commons.io.FileUtils;public class A1
{public static void main(String[] args
) {String filePath1
= "F:/dd" ;File file1
= new File( filePath1
) ;String filePath2
= "F:/ee" ;File file2
= new File( filePath2
) ;try {FileUtils.copyDirectory( file1
, file2
);} catch (IOException e
) {e
.printStackTrace();}}
String filePath1
= "F:/123.gif" ;
String filePath2
= "F:/abc.gif" ;File file1
= new File( filePath1
) ;
File file2
= new File( filePath2
) ;
FileUtils.copyFile( file1
, file2
);
- 下載文件 copyURLToFile(final URL source, final File destination)
package com.app;import java.io.File;
import java.io.IOException;
import java.net.URL
;import org.apache.commons.io.FileUtils;public class A8
{public static void main(String[] args
) {String url
= "http://imgsrc.baidu.com/baike/pic/item/7aec54e736d12f2ee289bffe4cc2d5628435689b.jpg" ;String filePath2
= "F:/abc.jpg" ;File file2
= new File( filePath2
) ;try {FileUtils.copyURLToFile( new URL( url
) , file2
);} catch (IOException e
) {e
.printStackTrace();}}
}
void writeStringToFile(final File file
, final String data
, final String encoding
)
void writeStringToFile(final File file
, final String data
, final Charset encoding
, final booleanappend
)
小例子
package com.app;import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;public class A8
{public static void main(String[] args
) {String mes
= "下班了" ;String filePath2
= "F:/abc.xml" ;File file2
= new File( filePath2
) ;try {FileUtils.writeStringToFile( file2
, mes
, "UTF-8" , true ); }catch (IOException e
) {e
.printStackTrace();}}
}
void writeByteArrayToFile(final File file
, final byte[] data
)void writeByteArrayToFile(final File file
, final byte[] data
, final boolean append
)void writeByteArrayToFile(final File file
, final byte[] data
, final int off
, final int len
)void writeByteArrayToFile(final File file
, final byte[] data
, final int off
, final int len
,final boolean append
)
小例子
package com.app;import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;public class A8
{public static void main(String[] args
) {String mes
= "哈哈,下班了" ;String filePath2
= "F:/abc.txt" ;File file2
= new File( filePath2
) ;try {byte[] mesArray
= mes
.getBytes() ;FileUtils.writeByteArrayToFile( file2
, mesArray
);}catch (IOException e
) {e
.printStackTrace();}}
}
void writeLines(final File file
, final Collection<?> lines
)void writeLines(final File file
, final Collection<?> lines
, final boolean append
)void writeLines(final File file
, final String encoding
, final Collection<?> lines
)void writeLines(final File file
, final String encoding
, final Collection<?> lines
,final boolean append
)void writeLines(final File file
, final String encoding
, final Collection<?> lines
,final String lineEnding
)void writeLines(final File file
, final String encoding
, final Collection<?> lines
,final String lineEnding
, final boolean append
)void writeLines(final File file
, final Collection<?> lines
, final String lineEnding
)void writeLines(final File file
, final Collection<?> lines
, final String lineEnding
,final boolean append
)
小例子1
package com.app;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;import org.apache.commons.io.FileUtils;public class A8
{public static void main(String[] args
) {ArrayList<String> list
= new ArrayList<>() ;String mes
= "哈哈,下班了" ;String mes2
= "回家,回家" ;list
.add( mes
) ; list
.add( mes2
) ; String filePath2
= "F:/abc.txt" ;File file2
= new File( filePath2
) ;try {FileUtils.writeLines( file2
, list
);}catch (IOException e
) {e
.printStackTrace();}}
}
結果:
小例子2
package com.app;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;import org.apache.commons.io.FileUtils;public class A8
{public static void main(String[] args
) {ArrayList<String> list
= new ArrayList<>() ;String mes
= "哈哈,下班了" ;String mes2
= "回家,回家" ;list
.add( mes
) ;list
.add( mes2
) ;String filePath2
= "F:/abc.txt" ;File file2
= new File( filePath2
) ;try {FileUtils.writeLines( file2
, list
, "。");}catch (IOException e
) {e
.printStackTrace();}}
}
結果:
void write(final File file
, final CharSequence data
, final Charset encoding
)void write(final File file
, final CharSequence data
, final String encoding
)void write(final File file
, final CharSequence data
, final Charset encoding
, final boolean append
)void write(final File file
, final CharSequence data
, final String encoding
, final boolean append
)
小例子
package com.app;
import java.io.File;
import java.io.IOException;import org.apache.commons.io.FileUtils;public class A8
{public static void main(String[] args
) {String mes
= "哈哈,下班了" ;String filePath2
= "F:/abc.txt" ;File file2
= new File( filePath2
) ;try {FileUtils.write( file2
, mes
, "utf-8" ,true );}catch (IOException e
) {e
.printStackTrace();}}
}
void moveDirectory(final File srcDir
, final File destDir
)
void moveDirectoryToDirectory(final File src
, final File destDir
, final boolean createDestDir
)
void moveFile(final File srcFile
, final File destFile
)
void moveFileToDirectory(final File srcFile
, final File destDir
, final boolean createDestDir
)
void moveToDirectory(final File src
, final File destDir
, final boolean createDestDir
)
void deleteDirectory(final File directory
)
void cleanDirectory(final File directory
)
void forceDelete(final File file
)
boolean deleteQuietly(final File file
)
void forceMkdir(final File directory
)
void forceMkdirParent(final File file
)
小例子1
package com.app;
import java.io.File;
import java.io.IOException;import org.apache.commons.io.FileUtils;public class A8
{public static void main(String[] args
) {String filePath
= "F:/123/abc/abc.txt" ;File file
= new File( filePath
) ;try {FileUtils.forceMkdir(file
);}catch (IOException e
) {e
.printStackTrace();}}
}
效果圖:
小例子1
package com.app;
import java.io.File;
import java.io.IOException;import org.apache.commons.io.FileUtils;public class A8
{public static void main(String[] args
) {String filePath
= "F:/123/abc/abc.txt" ;File file
= new File( filePath
) ;try {FileUtils.forceMkdirParent(file
);}catch (IOException e
) {e
.printStackTrace();}}
}
效果:
FileInputStream openInputStream(final File file
)
FileOutputStream openOutputStream(final File file
)
byte[] readFileToByteArray(final File file
)
String readFileToString(final File file
, final Charset encoding
)
String readFileToString(final File file
, final String encoding
)
List<String> readLines(final File file
, final Charset encoding
)
List<String> readLines(final File file
, final String encoding
)
boolean isFileNewer(final File file
, final File reference
)
boolean isFileNewer(final File file
, final Date date
)
boolean isFileNewer(final File file
, final long timeMillis
)
boolean isFileOlder(final File file
, final File reference
)
boolean isFileOlder(final File file
, final Date date
)
boolean isFileOlder(final File file
, final long timeMillis
)
boolean directoryContains(final File directory
, final File child
)
long sizeOf(final File file
)
File getTempDirectory()
String getTempDirectoryPath()
File getUserDirectory()
static String getUserDirectoryPath()
void touch(final File file
)
boolean contentEquals(final File file1
, final File file2
)
小例子
package com.app;
import java.io.File;
import java.io.IOException;import org.apache.commons.io.FileUtils;public class A8
{public static void main(String[] args
) {String filePath
= "F:/123" ;File file
= new File( filePath
) ;File child
= new File("F:/123/abc/123.txt") ;try {boolean hasChild
= FileUtils.directoryContains( file
, child
) ;System.out
.println( hasChild
);}catch (IOException e
) {e
.printStackTrace();}}
}
效果圖
個人微信號:zhaoyanjun125 , 歡迎關注
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-C4nDblzT-1599192189448)(http://o7rvuansr.bkt.clouddn.com/weixin200.jpg)]
總結
以上是生活随笔為你收集整理的Java IO流学习总结七:Commons IO 2.5-FileUtils的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。