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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

Java 文件复制 Hutool IO使用

發(fā)布時(shí)間:2025/3/19 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。