この記事では、参照用のJavaファイルの読み取りツールの特定のコードを共有しています。特定のコンテンツは次のとおりです
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.io.io.io.io.io.io.io.io.fileder; java.io.ioexception; Import java.io.inputStream; Import java.io.inputStreamReader; Import java.outputStream; Import java.io.io.io.urlencoder; Import java.net.urlencoder; Import Java.util.ArrayList; Import Java.util.util.calendar; java.util.hashmap; Import java.util.hashset; Import java.util.list; Import javax.servlet.http.httpservletresponse;/** * <p>ファイル操作ツールクラス<p> * @version 1.0 * @author li_hao @date 1月18日*/ @suppresswarnings({"resource"、 "unused"})public class fileutils {/** * Windows/linuxのプロジェクトルートディレクトリを取得 * @return */public static string getContextPath(){string fileurl = thread.currentthread()。getContextClasser()。 if( "usr" .equals(fileurl.substring(1,4))){fileurl.substring(0、fileurl.length() - 16); // linux} else {fileurl.substring(1、fileurl.length()-16); } / *** string to array* @param str string* @param splitstr delimiter* @return* / public static string [] stringtoarray(string str、string splittr){string [] arraystr = null; if(! ""。equals(str)&& str!= null){if(str.indexof(splittr)!= -1){arraysstr = str.split(splitstr); } else {arrayStr = new String [1]; ArrayStr [0] = str; }} return arraystr; } / ** * Fileを読み取り * * @param Path * @return * / public static String readfile(string path){bufferedreader reader = null;文字列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(); }最後に{if(reader!= null){try {reader.close(); } catch(ioexception e){e.printstacktrace(); }}} lastStrを返します。 } /** *フォルダー +ファジークエリ内のすべてのファイルの名前を取得します(ファジークエリが不要な場合、querystrを空またはnullに渡すことができます) * 1。パスが存在しない場合、マップは1 * 2です。 MAP RETURNS RETTYPE値は3、Filename List FilenamElist、Foldername List FolderNameList * @Param FolderPath Path * @Param QueryStr Fuzzy Query String */ public Static Hashmap <String、Object> getFilesName(String FolderPath、String QueryStr){Hashmap <String> Map <>;リスト<String> filenameList = new ArrayList <>(); //ファイル名リスト<String> FolderNameList = new ArrayList <>(); //フォルダー名リストファイルf = newファイル(FolderPath); if(!f.exists()){//パスは存在しませんmap.put( "rettype"、 "1"); } else {boolean flag = f.isdirectory(); if(flag == false){//パスはファイルmap.put( "rettype"、 "2"); map.put( "filename"、f.getname()); } else {//パスはフォルダーmap.put( "rettype"、 "3");ファイルfa [] = f.listfiles(); querystr = querystr == null? "":querystr; // querystrがnullに渡された場合、(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); }}マップを返します。 } / ** *動作単位のファイルを読み取り、最後の行に読み取ります * @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; // nullがファイルの終了になるまで一度に1行を読み取ります((tempstring = reader.readline()!= null){listcontent.add(tempstring); line ++; } reader.close(); } catch(ioexception e){e.printstacktrace(); }最後に{if(reader!= null){try {reader.close(); } catch(ioexception e1){}}} return listcontent; } / ** *指定された行データを読み取ります。注:0はスタート行 * @param filepath * @param linenumber * @return * / public static string readlinecontent(string filepath、int linenumber){bufferedreader reader = null;文字列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(); }最後に{if(reader!= null){try {reader.close(); } catch(ioexception e1){}} return linecontent; } / *** beginlineからendline(beginline and endlineを含む)からデータを読み取ります。注:0はスタートライン* @param filepath* @param beginlinenumber start line* @param endlinenumber end line* @return* / public static list <string> readlinescontent(string filepath、int beginlinenumber、int endlinumber) {int count = 0; BufferedReader Reader = new BufferedReader(new FileReader(filepath));文字列content = reader.readline(); while(content!= null){if(count> = beginlineNumber && count <= endlineNumber){listcontent.add(content); } content = reader.readline(); count ++; }} catch(例外e){} return listcontent; } / ** *いくつかのファイルですべてのデータを読み取る * @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; // nullがファイルの終了になるまで一度に1行を読み取ります((tempstring = reader.readline()!= null){listcontent.add(tempstring); line ++; } reader.close(); } catch(ioexception e){e.printstacktrace(); }最後に{if(reader!= null){try {reader.close(); } catch(ioexception e1){}}}} return listcontent; } /** *ファイルデータは書き込まれます(フォルダーとファイルが存在しない場合、最初に作成されてから作成されます) false:ファイルが存在し、内容が存在する場合、内容が交換されます*/ public static string filelineswrite(string filepath、string content、boolean flag){string filedo = "write"; filewriter fw = null; try {file file = new file(filepath); //フォルダーが存在しない場合、フォルダーを作成します(!file.getParentFile()。exists()){file.getParentFile()。mkdirs(); } if(!file.exists()){//ファイルが存在しない場合は、ファイルを作成して、content file.createNewfile()の最初の行を書き込みます。 fw = new filewriter(file); filedo = "create"; } else {//ファイルが存在する場合、コンテンツを追加または交換する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 writeintintoout(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(); }最後に{try {out.flush(); ins.close(); out.close(); } catch(ioexception e){e.printstacktrace(); }}} / ** *リスト内の要素がまったく同じかどうかを判断します(まったく同じ場合はtrue、return false) return 1 == new Hashset <Object>(list).size(); } / ** *リストに重複要素があるかどうかを判断します(複製がない場合はtrueを返し、誤りを返します) return list.size()== new Hashset <Object>(list).size(); }/** *日数の増加/減少 * @param date * @param num * @return */public static date dateadorsub(日付、int num){calendar startdt = calendar.getinstance(); startdt.settime(date); startdt.add(calendar.day_of_month、num); return startdt.getTime(); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。