Java file read/write tool sharing, java read/write tool class
The examples in this article share the code of the java file read/write tool class for your reference. The specific content is as follows:
Import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. fileReader; import java. io. fileWriter; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. io. outputStream; import java. io. printWriter; import java.net. URLEncoder; import java. util. arrayList; I Mport java. util. calendar; import java. util. date; import java. util. hashMap; import java. util. hashSet; import java. util. list; import javax. servlet. http. httpServletResponse;/*** <p> file operation tool class <p> * @ version 1.0 * @ author li_hao * @ date February */@ SuppressWarnings ({"resource ", "unused"}) public class FileUtils {/*** get the project root directory for windows/linux * @ return */public static String getConTextPath () {String fileUrl = Thread. currentThread (). getContextClassLoader (). getResource (""). getPath (); if ("usr ". equals (fileUrl. substring (1, 4) {fileUrl = (fileUrl. substring (0, fileUrl. length ()-16); // linux} else {fileUrl = (fileUrl. substring (1, fileUrl. length ()-16); // windows} return fileUrl ;} /*** String to array * @ param str String * @ param splitStr separator * @ return */public static String [] StringToArray (String str, String splitStr) {St Ring [] arrayStr = null; if (! "". Equals (str) & str! = Null) {if (str. indexOf (splitStr )! =-1) {arrayStr = str. split (splitStr);} else {arrayStr = new String [1]; arrayStr [0] = str ;}} return arrayStr ;} /*** read the file ** @ param Path * @ return */public static String ReadFile (String Path) {BufferedReader reader = null; String laststr = ""; try {FileInputStream fileInputStream = new FileInputStream (Path); InputStreamReader inputStreamReader = new InputStreamReader (fileInputStream, "UTF-8 "); Reader = new BufferedReader (inputStreamReader); String tempString = null; while (tempString = reader. readLine ())! = Null) {laststr + = tempString;} reader. close ();} catch (IOException e) {e. printStackTrace ();} finally {if (reader! = Null) {try {reader. close ();} catch (IOException e) {e. printStackTrace () ;}}return laststr;}/*** get the names of all files in the folder + fuzzy query (when fuzzy query is not required, queryStr is null or null) * 1. if the path does not exist, map returns a retType value of 1*2. when the path is the file path, map returns a retType value of 2, and the fileName value of the file name is the file name * 3. if a folder exists in the path, map returns the retType value of 3. The file name list is fileNameList, folder name list folderNameList * @ param folderPath path * @ param queryStr fuzzy query String * @ return */public static HashMap <String, Object> GetFilesName (String folderPath, String queryStr) {HashMap <String, Object> map = new HashMap <> (); List <String> fileNameList = new ArrayList <> (); // List of File names <String> folderNameList = new ArrayList <> (); // List of folder names File f = new File (folderPath); if (! F. exists () {// The path does not exist map. put ("retType", "1");} else {boolean flag = f. isDirectory (); if (flag = false) {// The path is the file map. put ("retType", "2"); map. put ("fileName", f. getName ();} else {// path is the folder map. put ("retType", "3"); File fa [] = f. listFiles (); queryStr = null? "": QueryStr; // If queryStr is input as null, replace it with null (indexOf matching value cannot be null) for (int I = 0; I <fa. length; I ++) {File fs = fa [I]; if (fs. getName (). indexOf (queryStr )! =-1) {if (fs. isDirectory () {folderNameList. add (fs. getName ();} else {fileNameList. add (fs. getName () ;}} map. put ("fileNameList", fileNameList); map. put ("folderNameList", folderNameList) ;}} return map ;}/ *** read the file in the unit of action, read the last line * @ param filePath * @ return */public static List <String> readFileContent (String filePath) {BufferedReader reader = null; list <String> listContent = new ArrayList <> (); Try {reader = new BufferedReader (new FileReader (filePath); String tempString = null; int line = 1; // read a row at a time, until null is read, the end of the file is while (tempString = reader. readLine ())! = Null) {listContent. add (tempString); line ++;} reader. close ();} catch (IOException e) {e. printStackTrace ();} finally {if (reader! = Null) {try {reader. close ();} catch (IOException e1) {}} return listContent;}/*** read data from the specified row. Note: 0 is the start line * @ param filePath * @ param lineNumber * @ return */public static String readLineContent (String filePath, int lineNumber) {BufferedReader reader = null; String lineContent = ""; try {reader = new BufferedReader (new FileReader (filePath); int line = 0; while (line <= lineNumber) {lineContent = reader. ReadLine (); line ++;} reader. close () ;}catch (IOException e) {e. printStackTrace () ;}finally {if (reader! = Null) {try {reader. close ();} catch (IOException e1) {}} return lineContent;}/*** read data from beginLine to endLine (including beginLine and endLine). Note: 0 is the start line * @ param filePath * @ param beginLineNumber start line * @ param endLineNumber end line * @ return */public static List <String> readLinesContent (String filePath, int beginLineNumber, int endLineNumber) {List <String> listContent = new ArrayList <> (); try {int count = 0; Bu FferedReader reader = new BufferedReader (new FileReader (filePath); String content = reader. readLine (); while (content! = Null) {if (count> = beginLineNumber & count <= endLineNumber) {listContent. add (content) ;}content = reader. readLine (); count ++ ;}} catch (Exception e) {} return listContent ;} /*** read all data in several files * @ param listFilePath * @ return */public static List <String> readFileContent_list (List <String> listFilePath) {List <String> listContent = new ArrayList <> (); for (String filePath: listFilePath) {File file = New File (filePath); BufferedReader reader = null; try {reader = new BufferedReader (new FileReader (file); String tempString = null; int line = 1; // read a row at a time until null is the end of the file while (tempString = reader. readLine ())! = Null) {listContent. add (tempString); line ++;} reader. close ();} catch (IOException e) {e. printStackTrace ();} finally {if (reader! = Null) {try {reader. close ();} catch (IOException e1) {}}} return listContent;}/*** write data to the file (if the folder and file do not exist, create and write the file first) * @ param filePath * @ param content * @ param flag true: if the file exists and the content exists, the content is appended with a new line. false: if the file exists and the content exists, the content is replaced with */public static String fileLinesWrite (String filePath, String content, boolean flag) {String filedo = "write"; FileWriter fw = null; try {File file = new File (filePath); // If the folder does not exist, Create the folder if (! File. getParentFile (). exists () {file. getParentFile (). mkdirs ();} if (! File. exists () {// if the file does not exist, create the file and write the first row of content file. createNewFile (); fw = new FileWriter (file); filedo = "create";} else {// if the file exists, append or replace the content fw = new FileWriter (file, flag) ;}} catch (IOException e) {e. printStackTrace ();} PrintWriter pw = new PrintWriter (fw); pw. println (content); pw. flush (); try {fw. flush (); pw. close (); fw. close ();} catch (IOException e) {e. printStackTrace ();} return filedo;}/*** write File * @ param ins * @ param out */public static void write0000out (InputStream ins, OutputStream out) {byte [] bb = new byte [10*1024]; try {int cnt = ins. read (bb); while (cnt> 0) {out. write (bb, 0, cnt); cnt = ins. read (bb) ;}} catch (IOException e) {e. printStackTrace ();} finally {try {out. flush (); ins. close (); out. close ();} catch (IOException e) {e. printStackTrace () ;}}/ *** determines whether the element in the list is Whether it is identical (true is returned if it is identical; otherwise false is returned) * @ param list * @ return */private static boolean hasSame (List <? Extends Object> list) {if (null = list) return false; return 1 = new HashSet <Object> (list ). size ();}/*** determines whether repeated elements exist in the list (true is returned if no repeated elements exist; otherwise, false is returned) * @ param list * @ return */private static boolean hasSame2 (List <? Extends Object> list) {if (null = list) return false; return list. size () = new HashSet <Object> (list ). size ();}/*** increase/decrease days * @ param date * @ param num * @ return */public static Date DateAddOrSub (Date date, int num) {Calendar startDT = Calendar. getInstance (); startDT. setTime (date); startDT. add (Calendar. DAY_OF_MONTH, num); return startDT. getTime ();}}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.