日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java common io_Java之Apache Commons-IO使用精讲

發布時間:2025/3/19 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java common io_Java之Apache Commons-IO使用精讲 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Commons IO是針對開發IO流功能的工具類庫。

主要包括六個區域:

工具類——使用靜態方法執行共同任務

輸入——用于InputStream和Reader實現

輸出——用于OutputStream和Writer實現

過濾器——各種文件過濾器實現

比較器——各種文件的java.util.Comparator實現

文件監聽器——監聽文件系統事件的組件

工具類

IOUtils

該工具類可能是平時使用得最多的工具類了。

IOUtils包含處理讀、寫和復制的工具方法。方法對InputStream、OutputStream、Reader和Writer起作用。

例如,從一個URL讀取字節的任務,并且打印它們:

public static void main(String[] args) throwsException {//從網絡上讀取一個網頁資源

InputStream in = new URL("http://commons.apache.org").openStream();try{

InputStreamReader inR= newInputStreamReader(in);

BufferedReader buf= newBufferedReader(inR);

String line;while ((line = buf.readLine()) != null) {

System.out.println(line);

}

}finally{if (in != null) {

in.close();

}

}

}//結果://控制臺打印出了這個網頁的所有內容

使用IOUtils:

public static void main(String[] args) throwsException {//從網絡上讀取一個網頁資源

try (InputStream in = new URL("http://commons.apache.org").openStream()) {

System.out.println(IOUtils.toString(in, StandardCharsets.UTF_8));

}//finally {//IOUtils.closeQuietly(in);//}

}

主要方式介紹:

buffer:一句話可以吧inputStream、outputStream、Reader、Witter等包裝成帶緩沖區的流,提高效率

closeQuietly:可以關閉各種流、socket等任何closeable的實例(不過官方推薦使用try-with-resources來代替)

contentEquals:比較兩個InputStream或者兩個Reader里面的內容(字節流)是否完全相同

public static void main(String[] args) throwsException {try (InputStream in1 = new URL("http://commons.apache.org").openStream(); InputStream in2 = new URL("http://commons.apache.org").openStream()) {

System.out.println(in1.equals(in2));//false

System.out.println(IOUtils.contentEquals(in1, in2)); //true

}

}

備注:contentEqualsIgnoreEOL(final Reader input1, final Reader input2) 該方法會忽略ignoring EOL characters

copy:流的互相拷貝??梢詫⑤斎肓骺降捷敵隽?。copy(final InputStream input, final OutputStream output, final int bufferSize),Reader拷貝到Writer等等

copyLarge:當你的流拷貝的是大文件(一般大于2G級別),請使用此方法拷貝

lineIterator:BufferedReader 通常在只有讀到空格或者換行符時才會結束讀取,攻擊者很容易構內存攻擊導致系統癱瘓,出于安全考慮這里推薦使用io包的LineIterator,并且其在性能上也優于普通流。

lineIterator(final InputStream input, finalCharset encoding)

lineIterator(final InputStream input, finalString encoding)

lineIterator(final Reader reader)

public static void main(String[] args) throwsException {try (InputStream in1 = new URL("http://commons.apache.org").openStream()) {

LineIterator lineIterator=IOUtils.lineIterator(in1, StandardCharsets.UTF_8);while(lineIterator.hasNext()) {

lineIterator.nextLine();

}

lineIterator.close();

}

}

read、readFully:把輸入流的東西讀取添加到第二個參數中的字節數組里

readLines:不解釋

resourceToByteArray、resourceToString:直接傳入一個文件的路徑,讀取進來

toBufferedInputStream:把普通的inputStream轉換成帶緩沖區的,返回一個新的InputStream

toByteArray:吧輸入流轉換到字節數組

toCharArray:

toInputStream:吧字符、字符串等等直接讀到流里

toString:強大的方法,可以吧各種輸出流讀成一個串

write、writeChunked、writeLines:把傳入的字節數組,寫入到輸出流里(可指定編碼)

各種常用的常量:

public static final char DIR_SEPARATOR_UNIX = '/';public static final char DIR_SEPARATOR_WINDOWS = '\\';/*** The system directory separator character. 系統文件夾的分隔符 通用的*/

public static final char DIR_SEPARATOR =File.separatorChar;/*** The Unix line separator string. 換行符*/

public static final String LINE_SEPARATOR_UNIX = "\n";//winows換行符

public static final String LINE_SEPARATOR_WINDOWS = "\r\n";/*** The system line separator string. 通用的換行符*/

public static final String LINE_SEPARATOR;

FilenameUtils

FilenameUtils類包含工具方法不需要使用File對象就可以操作文件名。該類致力于屏蔽Unix和Windows之間的不同,避免這些環境之間的轉換(例如,從開發到生產)。 開發在windows、生產在Linux

一般使用較少,這里不做過多介紹.

FileSystemUtils:2.6版本已經廢棄。推薦使用JDK自己的FileStore代替

總結

以上是生活随笔為你收集整理的java common io_Java之Apache Commons-IO使用精讲的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。