java第三方ApacheIO包
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.io.filefilter.IOFileFilter;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Scanner;
/**
* ApacheIO包
* www.apache.org提供的開源項目Commons中的IO包
*/
public class ApacheCommonsIO1 {
? ?public static void main(String[] args) {
? ? ? ?try {
? ? ? ? ? ?String s = FileUtils.readFileToString(new File("iostream/testFile.txt"),"utf-8");
? ? ? ? ? ?//FileUtils工具包,所有方法都為靜態(tài)方法可用類直接調(diào)用,.readFileToString(File f,String charset)方法需要傳入File對象和指定的字符集,將文件中的內(nèi)容轉(zhuǎn)換為字符串
? ? ? ? ? ?//java中字節(jié)流轉(zhuǎn)字符流同樣需要字符集,當沒有指定字符集時默認使用utf-8
? ? ? ? ? ?//InputStreamReader isr = new InputStreamReader(new FileInputStream("fas"),"utf-8");使用轉(zhuǎn)換流時如果只傳InputStream對象一個參數(shù)會默認使用utf-8,兩個參數(shù)時可對第二個參數(shù)設(shè)定字符集
? ? ? ? ? ?System.out.println(s);
? ? ? ? ? ?//將所有內(nèi)容全部讀出
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?try {
? ? ? ? ? ?FileUtils.copyDirectory(new File("c:/Aa"), new File("c:/Bb"), new FileFilter() {
? ? ? ? ? ? ? ?//.copyDirectory(File srcDir,File destDir,FileFilter filter)將源目錄src內(nèi)的內(nèi)容全部拷貝到目標目錄dest,如果目標目錄不存在會創(chuàng)建該目錄,F(xiàn)ileFilter接口用于篩選文件filter過濾器,使用過濾器僅拷貝篩選的文件
? ? ? ? ? ? ? ?@Override
? ? ? ? ? ? ? ?public boolean accept(File pathname) {
? ? ? ? ? ? ? ? ? ?//匿名內(nèi)部類,需要實現(xiàn)FileFilter接口的方法accept(File pathname),pathname為src目錄內(nèi)的每一個文件,當一個文件符合篩選條件時返回true執(zhí)行拷貝,返回false跳過該文件
? ? ? ? ? ? ? ? ? ?String name = pathname.getName();
? ? ? ? ? ? ? ? ? ?return name.endsWith(".doc") || name.endsWith(".ppt") || pathname.isDirectory();
? ? ? ? ? ? ? ? ? ?//當文件是doc/ppt/目錄時返回true,簡寫為return 判斷結(jié)果
? ? ? ? ? ? ? ? ? ?//如果不允許拷貝目錄則不會對Aa目錄的子目錄內(nèi)的內(nèi)容進行拷貝
? ? ? ? ? ? ? ?}
? ? ? ? ? ?});
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?try {
? ? ? ? ? ?FileUtils.cleanDirectory(new File("c:/Bb"));
? ? ? ? ? ?//.cleanDirectory()清空目錄內(nèi)的內(nèi)容
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?try {
? ? ? ? ? ?boolean b = FileUtils.contentEquals(new File("C:/Aa/a.txt"), new File("C:/Bb/b.txt"));
? ? ? ? ? ?//比較兩個文件的內(nèi)容是否相同
? ? ? ? ? ?System.out.println(b);
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?try {
? ? ? ? ? ?FileUtils.copyFile(new File("C:/Aa/001.jpg"),new File("c:/Bb/we/001.jpg"));
? ? ? ? ? ?//拷貝文件,如果目標路徑中某級目錄不存在會創(chuàng)建目錄樹
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?try {
? ? ? ? ? ?FileUtils.copyFileToDirectory(new File("C:/Aa/001.jpg"),new File("c:/Bb/wd"));
? ? ? ? ? ?//拷貝文件到目錄,和copyFile的區(qū)別是只能指定目的地目錄而不能改變文件名
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?Scanner sc = new Scanner(System.in);
? ? ? ?System.out.print("你有權(quán)保持沉默。如果你不保持沉默,那么你所說的一切都能夠用作為你的呈堂證供。你有什么要說的嗎:");
? ? ? ?String content = sc.nextLine();
? ? ? ?ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes());
? ? ? ?try {
? ? ? ? ? ?FileUtils.copyInputStreamToFile(bais,new File("c:/Bb/userInput.txt"));
? ? ? ? ? ?//拷貝輸入流中的內(nèi)容到文件,如果文件已存在會覆蓋文件
? ? ? ? ? ?System.out.println("你的供述已被保存至c:/Bb/userInput.txt文件中");
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?try {
? ? ? ? ? ?FileUtils.deleteDirectory(new File("c:/Bb/we"));
? ? ? ? ? ?//刪除目錄,無論目錄內(nèi)是否有內(nèi)容都會將目錄整個刪除
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?boolean b = FileUtils.deleteQuietly(new File("c:/Bb/userInput.txt"));
? ? ? ?//刪除文件,返回是否成功
? ? ? ?System.out.println(b);
? ? ? ?Collection<File> files = FileUtils.listFiles(new File("c:/Aa"), new IOFileFilter() {
? ? ? ? ? ?//.listFiles(File dir,IOFileFilter fileFilter,IOFileFilter dirFilter)返回dir目錄下文件File對象的集合,fileFilter篩選文件不能為null,dirFilter篩選目錄為null時不搜索子目錄
? ? ? ? ? ?@Override
? ? ? ? ? ?public boolean accept(File file) {
? ? ? ? ? ? ? ?return true;
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public boolean accept(File file, String s) {
? ? ? ? ? ? ? ?return true;
? ? ? ? ? ?}
? ? ? ?}, new IOFileFilter() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public boolean accept(File file) {
? ? ? ? ? ? ? ?return true;
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public boolean accept(File file, String s) {
? ? ? ? ? ? ? ?return true;
? ? ? ? ? ?}
? ? ? ?});
? ? ? ?System.out.println(files);
? ? ? ?//結(jié)果為:[c:\Aa\a\dfsadfs.ppt, c:\Aa\a\dsadsf.bmp, c:\Aa\fasd.doc] File對象集合中不包含目錄的對象
? ? ? ?FileInputStream fis=null;
? ? ? ?try {
? ? ? ? ? ?fis = FileUtils.openInputStream(new File("c:/Aa/fasd.doc"));
? ? ? ? ? ?//打開指定文件的輸入流FileInputStream
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}finally {
? ? ? ? ? ?if (fis != null) {
? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ?fis.close();
? ? ? ? ? ? ? ?} catch (IOException e) {
? ? ? ? ? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?try {
? ? ? ? ? ?List<String> strings = FileUtils.readLines(new File("iostream/testFile.txt"), "utf-8");
? ? ? ? ? ?//.readLines(File,charset)將文件每一行內(nèi)容作為一個字符串組成List<String>對象返回
? ? ? ? ? ?System.out.println(strings);
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?System.out.println(FileUtils.sizeOf(new File("c:/Aa"))/1024);
? ? ? ?//.sizeOf(File)返回文件的大小/字節(jié)數(shù),long類型
? ? ? ?try {
? ? ? ? ? ?FileUtils.write(new File("iostream/testFile.txt"),"1111","utf-8");
? ? ? ? ? ?//.write(File,data,charset)將data字符序列直接寫入文件中,會覆蓋原文件
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?byte[] content2 = "將字節(jié)數(shù)據(jù)寫入文件".getBytes();
? ? ? ?try {
? ? ? ? ? ?FileUtils.writeByteArrayToFile(new File("iostream/testFile.txt"),content2,true);
? ? ? ? ? ?//.writeByteArrayToFile(File,content,boolean append),append=true會在原文件后續(xù)寫,false或不傳第三個參數(shù)會覆蓋
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?ArrayList<Integer> arr = new ArrayList<>();
? ? ? ?arr.add(1);
? ? ? ?arr.add(0);
? ? ? ?arr.add(6);
? ? ? ?try {
? ? ? ? ? ?FileUtils.writeLines(new File("iostream/testFile.txt"),arr,true);
? ? ? ? ? ?//.writeLines(File,Collection,boolean append)將Collection集合寫入文件,每一個元素寫入.toString()的字符串
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?try {
? ? ? ? ? ?FileUtils.writeStringToFile(new File("iostream/testFile.txt"),"直接將字符串寫入文件","utf-8",true);
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?//IOUtils工具類
? ? ? ?BufferedInputStream bis=null;
? ? ? ?fis=null;
? ? ? ?BufferedOutputStream bos=null;
? ? ? ?try {
? ? ? ? ? ?bis = IOUtils.buffer(new FileInputStream("iostream/testFile.txt"));
? ? ? ? ? ?//.buffer(Stream)包裝一層緩沖流返回,選填第二個參數(shù)size指定緩沖大小
? ? ? ? ? ?bos = IOUtils.buffer(new FileOutputStream("iostream/testFile2.txt"));
? ? ? ? ? ?int copy = IOUtils.copy(bis, bos);
? ? ? ? ? ?//.copy(InputStream/Reader,OutputStream/Writer),將輸入流的內(nèi)容拷貝到輸出流,返回拷貝的字節(jié)數(shù)
? ? ? ? ? ?//.copyLarge()拷貝大于2G的內(nèi)容時使用
? ? ? ? ? ?fis = new FileInputStream("iostream/testFile2.txt");
? ? ? ? ? ?boolean bb = IOUtils.contentEquals(bis, fis);
? ? ? ? ? ?//比較兩個輸入流中的內(nèi)容是否一致
? ? ? ? ? ?System.out.println(bb);
? ? ? ?} catch (FileNotFoundException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?} finally {
? ? ? ? ? ?IOUtils.closeQuietly(bis,fis,bos);
? ? ? ? ? ?//.closeQuietly(Stream)關(guān)閉流,可變參數(shù)
? ? ? ?}
? ? ? ?FileReader fr = null;
? ? ? ?FileWriter fw = null;
? ? ? ?try {
? ? ? ? ? ?fr = new FileReader("iostream/testFile2.txt");
? ? ? ? ? ?LineIterator iterator = IOUtils.lineIterator(fr);
? ? ? ? ? ?//.lineIterator(InputStream/Reader)返回一個輸入流的逐行迭代器,每一行都不包含換行/r/n
? ? ? ? ? ?while (iterator.hasNext()){
? ? ? ? ? ? ? ?System.out.println(iterator.next());
? ? ? ? ? ?}
? ? ? ? ? ?char[] buff = new char[100];
? ? ? ? ? ?int num = IOUtils.read(fr, buff);
? ? ? ? ? ?//.read()將輸入流中的內(nèi)容讀到數(shù)組中,字符流對char[],字節(jié)流對byte[],返回讀入數(shù)組的字符/字節(jié)數(shù)
? ? ? ? ? ?System.out.println(num);
? ? ? ? ? ?//結(jié)果為32
? ? ? ? ? ?IOUtils.readFully(fr,buff);
? ? ? ? ? ?//.readFully()將輸入流的內(nèi)容填滿數(shù)組,如果數(shù)組填不滿會拋EOFException: Length to read: 100 actual: 32
? ? ? ? ? ?List<String> lines = IOUtils.readLines(fr);
? ? ? ? ? ?//.readLines()返回每一行字符串的集合
? ? ? ? ? ?BufferedReader br = IOUtils.toBufferedReader(fr);
? ? ? ? ? ?//.toBufferedReader/InputStream包裝緩沖流
? ? ? ? ? ?byte[] bytes = IOUtils.toByteArray(fr, "utf-8");
? ? ? ? ? ?//.toByteArray()/.toCharArray()將輸入流的內(nèi)容轉(zhuǎn)為字節(jié)數(shù)組或字符數(shù)組
? ? ? ? ? ?System.out.println(IOUtils.toString(fr));
? ? ? ? ? ?//.toString()將輸入流或字節(jié)數(shù)組轉(zhuǎn)化為字符串,非字符流需要指定字符集
? ? ? ? ? ?fw = new FileWriter("iostream/testFile2.txt");
? ? ? ? ? ?IOUtils.write("將內(nèi)容寫進輸出流",fw);
? ? ? ? ? ?arr = new ArrayList<>();
? ? ? ? ? ?arr.add(1);
? ? ? ? ? ?arr.add(1);
? ? ? ? ? ?arr.add(1);
? ? ? ? ? ?IOUtils.writeLines(arr,"自定義每行的行尾,null默認為換行符,如果自定義不加換行符會全都寫在一行",fw);
? ? ? ? ? ?//.writeLines(Collection<?>,String lineEnding,OutputStream/Writer)寫入任意集合的每個元素的toString字符串
? ? ? ?} catch (FileNotFoundException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?} finally {
? ? ? ? ? ?IOUtils.closeQuietly(fr,fw);
? ? ? ?}
? ?}
}