이 기사는 참조를위한 특정 Java 파일 판독 및 쓰기 도구의 특정 코드를 공유합니다. 특정 내용은 다음과 같습니다
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.filereader; java.io.filewriter; 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; import java.util.calendar; import java.util; 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 18, 2017 */ @suppresswarnings ({ "resource", "unsused"}) public class fileUtils {/** * Windows/Linux의 프로젝트 루트 디렉토리 가져 오기 */public static string getContextPath () {String FileUrl = Thread.CurrentThread (). getContextClassLoader (). getResource ( "). if ( "usr".equals (fileurl.substring (1,4))) {fileUrl = (fileUrl.subString (0, fileUrl.length () -16)); // linux} else {fileUrl = (fileUrl.subString (1, fileUrl.length () -16)); } / *** 배열에 대한 문자열* @param str string* @param splittr delimiter* @return* / public static string [] StringToArray (String Str, String Splitstr) {String [] arraystr = null; if (! "". equals (str) && str! = null) {if (str.indexof (splitst)! = -1) {arraystr = str.split (splitst); } else {arrayst = new String [1]; Arraystr [0] = str; }} return arraystr; } / ** * 읽기 파일 * * @param path * @return * / public static string readfile (String Path) {bufferedReader reader = null; 문자열 laststr = ""; {fileInputStream fileInputStream = new FileInputStream (Path); inputStreamReader inputStreamReader = new inputStreamReader (FileInputStream, "UTF-8"); reader = new bufferedReader (inputStreamReader); 문자열 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 (); }}} return laststr; } /** * 폴더 + 퍼지 쿼리에서 모든 파일의 이름을 가져옵니다 (퍼지 쿼리가 필요하지 않은 경우 쿼리 스트리트가 비어 있거나 널을 전달할 수 있습니다) * 1. 경로가 존재하지 않으면 맵 값을 반환합니다. rettype 값은 1 * 2입니다. 경로가 파일 경로 인 경우, 맵 값이 2, 폴더 이름의 폴더 값이되면, 맵은 필레나 이름 * 3입니다. rettype 값은 3, 파일 이름 목록 FilenameList, 폴더 이름 목록 폴더 이름 목록 * @param 폴더 포트 경로 * @param querystr fuzzy quzzy quzer string * @return */ public static hashmap <string, object> getfilesname (String folderPath, String Querystr) {hashmap <string, map = new Hashmap <(); List <string> filenamelist = new ArrayList <> (); // 파일 이름 목록 목록 <String> FolderNamelist = New ArrayList <> (); // 폴더 이름 목록 파일 F = 새 파일 (FolderPath); if (! f.exists ()) {// 경로는 존재하지 않습니다. } else {부울 플래그 = 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)); 문자열 tempstring = null; int line = 1; // null이 파일의 끝이 될 때까지 한 번에 한 줄을 읽습니다. 라인 ++; } reader.close (); } catch (ioexception e) {e.printstacktrace (); } 마침내 {if (reader! = null) {try {reader.close (); } catch (ioexception e1) {}}} return listContent; } / ** * 지정된 행 데이터를 읽으십시오. 참고 : 0은 시작 줄 * @param filepath * @param linenumbe * @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 (); 라인 ++; } reader.close (); } catch (ioexception e) {e.printstacktrace (); } 마침내 {if (reader! = null) {try {reader.close (); } catch (ioexception e1) {}} return linecontent; } / *** 초기 라인에서 엔드 라인까지 (초보자 및 끝선 포함), 참고 : 0은 시작 선* @param filepath* @param steglineNument start line* @param endlineNumber end line* @return* / public static list <string> readlinescontent (String filepath, int beginlineNumber, int endlineNumber) {list <string> listcontcontcontcontcontcontcontcontront <string> listcontcontcontront <string <strenglineNumber, int endlineRIMBER); {int count = 0; BufferedReader reader = New BufferedReader (New Filereader (FilePath)); 문자열 content = reader.readline (); while (content! = null) {if (count> = steglineNumber && count <= endlineNumber) {listContent.add (content); } content = reader.readline (); 카운트 ++; }} 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 (파일)); 문자열 tempstring = null; int line = 1; // null이 파일의 끝이 될 때까지 한 번에 한 줄을 읽습니다. 라인 ++; } reader.close (); } catch (ioexception e) {e.printstacktrace (); } 마침내 {if (reader! = null) {try {reader.close (); } catch (ioException e1) {}}}} return listContent; } /** * 파일 데이터가 작성되었습니다 (폴더와 파일이 존재하지 않으면 먼저 생성 된 다음 작성) * @param filepath * @param content * @param flag true : 파일이 존재하고 내용이 존재하면 콘텐츠가 새 줄에 추가됩니다. FALSE : 파일이 존재하고 내용이 존재하면 내용이 대체됩니다*/ public static String filLinesWrite (문자열 FilePath, String Content, Boolean Flag) {String Filedo = "쓰기"; filewriter fw = null; try {file file = new File (FilePath); // 폴더가 존재하지 않으면 (! file.getParentFile (). Exists ()) {file.getParentFile (). mkdirs (); } if (! file.exists ()) {// 파일이 존재하지 않으면 파일을 작성하고 첫 번째 컨텐츠 파일을 작성하십시오 .createnewFile (); fw = 새로운 filewriter (파일); filedo = "Create"; } else {// 파일이 존재하는 경우 컨텐츠를 추가하거나 교체합니다. }} catch (ioexception e) {e.printstacktrace (); } printwriter pw = 새로운 printwriter (fw); pw.println (컨텐츠); pw.flush (); try {fw.flush (); pw.close (); fw.close (); } catch (ioexception e) {e.printstacktrace (); } return filedo; } / ** * 쓰기 파일 * @param ins * @param out * / public static void writeIntoout (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 (); }}}} / ** * 목록의 요소가 정확히 동일인지 확인하십시오 (정확히 동일하게 반환합니다. return 1 == New Hashset <botor> (list) .size (); } / ** * 목록에 중복 요소가 있는지 여부를 결정하십시오 (복제가 없으면 true, 그렇지 않으면 false를 반환) * @param list * @return * / private static boolean hassame2 (list <? extends object> list> list) {if (null == list) return false; return list.size () == New Hashset <botor> (list) .size (); }/** * 일의 수를 늘리기/감소 * @param 날짜 * @param num * @return */public static date dateaddorsub (날짜 날짜, int num) {calendar startdt = calendar.getinstance (); startdt.settime (날짜); startdt.add (calendar.day_of_month, num); return startdt.gettime (); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.