Java 文件复制 Hutool IO使用
生活随笔
收集整理的這篇文章主要介紹了
Java 文件复制 Hutool IO使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文件讀寫(xiě)操作
我要將這張照片復(fù)制一份。
原生Java代碼方式
package com.hutool; import java.io.*; /*** @Author: crush* @Date: 2021-05-20 19:21* version 1.0*/ public class HuToolIoDemo {public static void main(String[] args) throws IOException {FileInputStream inputStream= new FileInputStream(new File("E:\\good_image\\image\\1.jpg"));FileOutputStream outputStream = new FileOutputStream(new File("E:\\good_image\\2.jpeg"));//定義一個(gè)緩沖byte[] b=new byte[1024];int len=0;while (true){len=inputStream.read(b);if (len==-1) {break;}outputStream.write(b,0,len);}inputStream.close();outputStream.close();} }是又要設(shè)置緩沖區(qū),又要寫(xiě)一個(gè)循環(huán)一個(gè)個(gè)去讀。
引入了hutool工具
但是如果引入了hutool之后,代碼變成了三行。
package com.hutool;import cn.hutool.core.io.IoUtil;import java.io.*;/*** @Author: crush* @Date: 2021-05-20 19:21* version 1.0*/ public class HuToolIoDemo {public static void main(String[] args) throws IOException {FileInputStream inputStream= new FileInputStream(new File("E:\\good_image\\image\\1.jpg"));FileOutputStream outputStream = new FileOutputStream(new File("E:\\good_image\\2.jpeg"));IoUtil.copy(inputStream,outputStream);} }頓時(shí)感覺(jué)真香。
hutool 有很多很好用的東西,轉(zhuǎn)換類(lèi)型哪方面也非常好用,建議去試一試。
大家如果用到很多hutool的工具 。
可以想我一樣使用下面這個(gè)依賴(lài)。全部引入。
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.6.5</version> </dependency>一些簡(jiǎn)單常用的類(lèi)型轉(zhuǎn)換
package com.hutool;import cn.hutool.core.convert.Convert; import java.util.Date; import java.util.List; /*** @Author: crush* @Date: 2021-05-20 19:43* version 1.0*/ public class HuToolDemo2 {public static void main(String[] args) {//轉(zhuǎn)換為字符串int a=1;System.out.println(Convert.toStr(a));long[] b={1,2,3,4,5};System.out.println(Convert.toStr(b));//轉(zhuǎn)換指定的類(lèi)型數(shù)組 結(jié)果轉(zhuǎn)為Integer 數(shù)組String[] sss={"1","2","3","4","5"};Integer[] integers = Convert.toIntArray(sss);//字符串轉(zhuǎn)對(duì)象String str1="2020-12-12";System.out.println(Convert.toDate(str1));String str2="2020/12/12";System.out.println(Convert.toDate(str2));String str3="2020.12.12";System.out.println(Convert.toDate(str3));// 數(shù)組轉(zhuǎn)集合String [] aaa={"111","222","第一次學(xué)習(xí)HuTool工具包","是真的強(qiáng)大"};List<String> objects = (List<String>) Convert.toList(aaa);System.out.println(objects);}}自言自語(yǔ)
學(xué)習(xí)的更多,才能發(fā)現(xiàn)更多的樂(lè)趣。
總結(jié)
以上是生活随笔為你收集整理的Java 文件复制 Hutool IO使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 史上最详细阿里云服务器上Docker部署
- 下一篇: java美元兑换,(Java实现) 美元