Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the JAVA file manipulation tool class?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

JAVA file operation tool class, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Java common file tools, regular text content read and write, file movement, file copy, file slicing, file appending and writing, file renaming, paging reading, etc.

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 File manipulation tool Class * @ 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 (newFile (filePath));} public static boolean changeFileName (String filePath, String newFileName) {return copyFile (filePath, newFile (filePath). GetParent () + File.separatorChar + newFileName) } / / split the file into multiple public static boolean splitFile (String filePath, int size) {if (size 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");}}

Description:

Anyone who has worked on a project knows that many of the reusable code blocks or useful tool classes that have been written have not been sorted out, and when needed, they have to open the project and look it up, although the function development is not difficult, but it takes time and cost to write and test, so it is better to keep the wheel for everyone to use.

After reading the above, have you mastered the method of JAVA file manipulation tool class? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report