用途:FileUtil 文件工具类(读取文件)
使用场景
读取文件内容的各种骚操作
项目引用
此博文的依据:hutool-5.6.5版本源码
方法明细
方法名称:cn.hutool.core.io.FileUtil.readBytes(java.io.File)
方法描述
读取文件所有数据<br>
文件的长度不能超过Integer.MAX_VALUE
支持版本及以上
参数描述:参考案例:
String path = "C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt"; //从文件中读取每一行的UTF-8编码数据 ArrayList<String> readUtf8Lines = FileUtil.readLines(new File(path),CharsetUtil.CHARSET_UTF_8, new ArrayList<>()); for(String readUtf8Line :readUtf8Lines){ System.out.println(readUtf8Line); }
public class FileUtilReaderHandler implements FileReader.ReaderHandler { @Override public Object handle(BufferedReader reader) throws IOException { System.out.println("FileUtilReaderHandler:start"); String str = null; List<String> stringList = new ArrayList<>(); //到达流末尾, 就返回null while((str = reader.readLine()) != null){ System.out.println(str); stringList.add(str); } return stringList; } } //------------------------ String path = "C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt"; List<String> list = (List<String>) FileUtil.load(path,CharsetUtil.CHARSET_UTF_8,new FileUtilReaderHandler()); for(String str :list){ System.out.println("loadUtf8Test:"+str); }
public class FileUtilReaderHandler implements FileReader.ReaderHandler { @Override public Object handle(BufferedReader reader) throws IOException { System.out.println("FileUtilReaderHandler:start"); String str = null; List<String> stringList = new ArrayList<>(); //到达流末尾, 就返回null while((str = reader.readLine()) != null){ System.out.println(str); stringList.add(str); } return stringList; } } //------------------------ String path = "C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt"; List<String> list = (List<String>) FileUtil.load(new File(path),CharsetUtil.CHARSET_UTF_8,new FileUtilReaderHandler()); for(String str :list){ System.out.println("loadUtf8Test:"+str); }