您好,登錄后才能下訂單哦!
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<String> readLines(String filePath) { return readLines(filePath,ENCODING); } public static List<String> 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<String> readPageLines(String filePath, String encoding, int startRow, int endRow) { List<String> 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<size;i++){ if (i == (size -1)){ byteSize += mSize;//將剩余加到最后一個文件里 } System.out.println("i="+i+",byteSize:"+byteSize); raf = new RandomAccessFile(file, "r"); raf.seek(i * byteSize); byte [] b = new byte[byteSize]; int s = raf.read(b); if (s > 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<String> fileList(String fileDir){ File file = new File(fileDir); if (!file.exists()){ return null; } List<String> 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文件操作工具類是什么的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。