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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOUtils常用方法的使用

發布時間:2023/12/20 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOUtils常用方法的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/**
* apache.commons.io.IOUtils常用方法的使用
* 對字節流inputStream,outStream,writer,reader字符流的常用方法的使用
* @author ZYY
*
*/
public class TestCommon {
public static void main(String[] args) throws IOException {
testLineIterator();
}
/**
* CloseQuietly可以關閉inputStream,outputStream,reader,writer流
*
* @throws IOException
*/
public static void testCloseQuietly() throws IOException {
String filename = “D:” + File.separator + “data.txt”;
File f = new File(filename);
InputStream in = new FileInputStream(f);
// 讀取內容
byte[] b = new byte[1024];
in.read(b);
System.out.println(new String(b));
OutputStream out = new FileOutputStream(f, true);
String str = “我們是幸運的”;
out.write(str.getBytes());
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
/**
* toString的使用
* @throws IOException
*/
public static void testToString() throws IOException {
String filename = “D:” + File.separator + “data.txt”;
File f = new File(filename);
InputStream in = new FileInputStream(f);
System.out.println(IOUtils.toString(in));
IOUtils.closeQuietly(in);
}
/**
* copy的使用
* copy能拷貝Integer.MAX_VALUE的字節數據,即2^31-1。
* 如果是很大的數據,那么可以選擇用copyLarge方法,適合拷貝較大的數據流,比如2G以上
* @throws IOException
*/
public static void testCopy() throws IOException {
String filename = “D:” + File.separator + “data.txt”;
String filename2=”D:”+File.separator+”data2.txt”;
File f = new File(filename);
File f2=new File(filename2);
InputStream input1=new FileInputStream(f2);
InputStream in = new FileInputStream(f);
System.out.println(“未復制之前:”+IOUtils.toString(input1));
OutputStream out=new FileOutputStream(f2);
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
InputStream input2=new FileInputStream(f2);
System.out.println(“復制之后:”+IOUtils.toString(input2));
IOUtils.closeQuietly(input1);
IOUtils.closeQuietly(input2);
}
/**
* ToByteArray的使用
* @throws IOException
*/
public static void testToByteArray() throws IOException {
String filename=”D:”+File.separator+”data.txt”;
File f=new File(filename);
InputStream in=new FileInputStream(f);
byte[] byteArray = IOUtils.toByteArray(in);
System.out.println(new String(byteArray));
IOUtils.closeQuietly(in);
}
/**
* 測試write
* @throws IOException
*/
public static void testWrite() throws IOException {
String filename=”D:”+File.separator+”data.txt”;
File f=new File(filename);
OutputStream out=new FileOutputStream(f);
IOUtils.write(“我們是幸運的”, out);
IOUtils.closeQuietly(out);
InputStream in=new FileInputStream(f);
System.out.println(IOUtils.toString(in));
IOUtils.closeQuietly(in);
}
/**
* 測試ToInputStream
* @throws IOException
*/
public static void testToInputStream() throws IOException {

InputStream inputStream = IOUtils.toInputStream("LHQ是幸運的");System.out.println(IOUtils.toString(inputStream)); IOUtils.closeQuietly(inputStream); } /*** 測試ReadLines* 分行讀取內容* @throws IOException*/ public static void testReadLines() throws IOException {String filename="D:"+File.separator+"data.txt";File f=new File(filename);Reader input=new FileReader(f);List readLines = IOUtils.readLines(input);for (Object object: readLines) {System.out.println(object);}IOUtils.closeQuietly(input); } /*** 測試LineIterator* LineIterator可以構造器可以接受一個Reader* 通常在讀取大文件時候將BufferedReader裝進去性能會比較好* @throws IOException*/ public static void testLineIterator() throws IOException {//于安全考慮這里推薦使用io包的LineIterator,并且其在性能上也優于普通流String filename="D:"+File.separator+"data.txt";File f=new File(filename);Reader reader=new FileReader(f);LineIterator lineIterator = IOUtils.lineIterator(reader);while (lineIterator.hasNext()) {System.out.println(lineIterator.nextLine());}lineIterator.close();IOUtils.closeQuietly(reader); }

}

總結

以上是生活随笔為你收集整理的IOUtils常用方法的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

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