JAVA文件操作工具类是什么
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,JAVA文件操作工具类是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。Java常用文件工具类,常规的文本内容读写、文件移动、文件复制
千家信息网最后更新 2026年11月03日JAVA文件操作工具类是什么
JAVA文件操作工具类是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
Java常用文件工具类,常规的文本内容读写、文件移动、文件复制、文件切分、文件追加写、文件更名、分页读取等;
FileUtils.java
import java.io.*;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.List;/** * @author JL * @version V1.0 * @Description 文件操作工具类 * @date 2019/06/14 */public class FileUtils { private static final String ENCODING = "UTF-8"; public static boolean createFile(String filePath){ File file = new File(filePath); try{ if (file.exists()){ return true; } File parentFile = file.getParentFile(); if (parentFile != null && parentFile.exists()){ return file.createNewFile(); } if (new File(file.getParent()).mkdirs()){ return file.createNewFile(); } }catch(IOException e){ e.printStackTrace(); } return false; } public static String readLine(String filePath, String encoding) { try { return org.apache.commons.io.FileUtils.readFileToString(new File(filePath), encoding); } catch (IOException e) { e.printStackTrace(); } return ""; } public static List readLines(String filePath) { return readLines(filePath,ENCODING); } public static List readLines(String filePath, String encoding) { try { return org.apache.commons.io.FileUtils.readLines(new File(filePath), encoding); } catch (IOException e) { e.printStackTrace(); } return null; } //分页读取文本内容 public static List readPageLines(String filePath, String encoding, int startRow, int endRow) { List lines = readLines(filePath, encoding); if (lines != null && lines.size()> endRow) { return lines.subList(startRow, endRow); } return lines; } public static boolean copyFile(String srcPath, String targetPath) { try { org.apache.commons.io.FileUtils.copyFile(new File(srcPath), new File(targetPath)); return true; } catch (IOException e) { e.printStackTrace(); } return false; } public static boolean moveFile(String srcPath, String targetPath) { try { org.apache.commons.io.FileUtils.moveFile(new File(srcPath), new File(targetPath)); return true; } catch (IOException e) { e.printStackTrace(); } return false; } public static boolean appendFile(String filePath, String content) { try { org.apache.commons.io.FileUtils.write(new File(filePath),content, ENCODING , true); return true; } catch (IOException e) { e.printStackTrace(); } return false; } public static boolean appendFile(String filePath, String ... contents) { return appendFile(filePath, Arrays.asList(contents)); } public static boolean appendFile(String filePath, Collection> lines) { if (lines != null){ try { org.apache.commons.io.FileUtils.writeLines(new File(filePath),lines, ENCODING, true); return true; } catch (IOException e) { e.printStackTrace(); } } return false; } public static boolean remove(String filePath){ return org.apache.commons.io.FileUtils.deleteQuietly(new File(filePath)); } public static boolean changeFileName(String filePath, String newFileName){ return copyFile(filePath, new File(filePath).getParent() + File.separatorChar + newFileName); } //将文件拆分多个 public static boolean splitFile(String filePath, int size){ if (size <= 0){ return false; } File file = new File(filePath); long length = file.length(); if (length < size){ return false; } int byteSize = (int)(length / (long) size);//均值 int mSize = (int)(length % (long) size);//取余 String fileName = file.getName(); String newFileName = fileName.substring(0,fileName.lastIndexOf(".")); String fileSuffix = filePath.substring(filePath.lastIndexOf(".")); System.out.println("fileName:"+fileName+",fileLength:"+length+",size:"+ size +",byteSize:"+byteSize+",mSize:"+mSize); RandomAccessFile raf = null; OutputStream os; try { for (int i=0;i 0) { os = new FileOutputStream(file.getParent() + File.separatorChar + newFileName + i + fileSuffix); os.write(b); os.flush(); os.close(); } } return true; }catch(IOException ioe) { ioe.printStackTrace(); }finally{ if (raf != null) { try { raf.close(); } catch (IOException ioe) { } } } return false; } //获取目录下所有文件(含所有子目录下的文件) public static List fileList(String fileDir){ File file = new File(fileDir); if (!file.exists()){ return null; } List fileList = new ArrayList<>(); (new FileInterface(){ @Override public void find(File fFile) { File [] files = null; if (fFile.isDirectory() && (files = fFile.listFiles()) != null){ for (File f : files) { find(f); } }else { fileList.add(fFile.getPath()); } } }).find(file); fileList.forEach(f-> System.out.println(f)); return fileList; } interface FileInterface{ void find(File fFile); } public static void main(String[] args) {// changeFileName("D:\\test\\test.txt","new.txt");// splitFile("D:\\test\\test.txt",3);// moveFile("D:\\test\\test.txt","F:\\test\\test.txt");// createFile("D:\\test\\ab\\11\\test.txt"); fileList("D:\\test"); }} 说明:
做过项目的人都知道,很多写过的可重复利用的代码块或有用的工具类没有怎么整理,当需要的时候,又得打开项目查找一翻,虽然功能开发不难,但是又得花时间成本去写去测试,这样的重复造轮子的事情太多次了;因此不如把轮子保留,供大家一起使用;
看完上述内容,你们掌握JAVA文件操作工具类是什么的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
文件
工具
内容
文本
方法
更多
轮子
问题
项目
束手无策
为此
事情
代码
功能
原因
均值
多个
子目
子目录
对此
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
花魁咖啡
科技互联网
上海喔噻互联网科技有限公司
天津市摩尔互联网科技
北京互联网科技
唐信互联网科技
互联网科技视频
成都互联网科技公司
朗涯互联网科技
上海喔噻互联网科技
兼蜜互联网科技
盛创互联网科技有限公司
华中科技有哪些互联网大佬
剑昆互联网络科技有限公司临桂
湖南诚度互联网科技有限公司
互联网方面的科技前沿
元气玛特互联网科技公司是外包吗
深圳无忧互联网科技
互联网安防科技感
世界互联网科技成果奖
互联网科技的
嘟嘟瓜互联网科技
左右互联网络科技
互联网科技产品
小米互联网科技园设置
互联网大会黑科技排名
移动互联网科技公司
深圳艾果互联网通讯科技
互联网与数字科技关系
科技互联网行业投资研究