파일은 Java IO에서 자주 사용되며 파일 업로드 및 삭제에 사용됩니다. 예를 들어, 관리 시스템에 글을 쓰면 사진 업로드 및 삭제를 사용할 수 있습니다. 그런 다음 Java 파일을 사용하여 처리합니다.
Java에서 파일의 기본 사용 파일을 생성하고 삭제합니다.
public class filedemo {public static void main (String [] args) {file f = new File ( "d :"+file.separator+"io.txt"); //file.separator gets "//file.pathseparator gets"; " try {f.createnewfile (); } catch (ioexception e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } // 등. 잠시 동안 파일 생성을 볼 수 있습니다. {thread.sleep (3000); } catch (InterruptedException e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } if (f.exists ()) {f.delete (); } else {System.out.println ( "파일이 존재하지 않음"); }}} Java 파일 예제 사용 : J2EE 개발에 사용되는 이미지 업로드 기능 코드 :
public void fileUpload (@requestparam multipartfile [] myFiles, httpservletRequest 요청, httpservletResponse 응답)는 ioexception {String imgpath = "/uploads" + "/"; 파일 디렉토리 = 새 파일 (request.getSession (). getServletContext () .getRealPath ( "/") + imgpath); 문자열 desfilename = null; 문자열 filenewname = null; Response.setContentType ( "Text/Html; charset = utf-8"); printwriter out = response.getwriter (); 문자열 OriginalFilename = null; for (multipartfile myfile : myfiles) {if (myfile.isempty ()) {out.write ( "파일을 선택하고 업로드하십시오"); out.flush (); } else {OriginalFileName = myFile.getoriginalFilename (); if (null! = OriginalFilename && originalfilename.length ()> 0) {filenewname = uuid.randomuuid () + ordignfilename; desfilename = directory.tostring () + "/" + filenewname; } try {fileUtils.copyInputStreamTofile (myfile.getInputStream (), 새 파일 (desfilename)); } catch (ioexception e) {e.printstacktrace (); out.write ( "파일 업로드 실패, 다시 시도하십시오 !!"); out.flush (); }}} out.print (filenewName); out.flush (); } 폴더에서 생성 된 코드는 다음과 같습니다.
file f1 = 새 파일 ( "d :"+file.separator+"test"); f1.mkdir (); // 폴더 이름을 가져 오는 메소드 f1.getName ();
이것은 Java IO의 기본 사용이며 더 자주 사용되는 부분입니다.
위의 내용은이 기사에 관한 모든 것입니다. 모든 사람의 학습에 도움이되기를 바랍니다.