PackageCom.io;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.ArrayList;Importjava.util.Date;ImportJava.util.HashMap;Importjava.util.Hashtable;Importjava.util.List;Importorg.junit.Test; Public classFiletest {/*** 1, File class object, corresponding to the physical one file * 2, the method in the file class, only related to how to create, modify, delete and so on the operation of the file, the operation of the file contents * Operation file content requires IO * 3, File class object, often as Parameters for specific classes of IO streams*/@Test Public voidfiletest () {File file=NewFile ("Hello1.txt");//to represent a file in physics as an object in JavaSystem.out.println (File.getabsolutepath ()); System.out.println (File.getparent ()); System.out.println (File.getpath ()); System.out.println (File.getname ()); File file1=NewFile ("Hello2.txt"); /*** Rename * requires file must exist, file1 must not exist for renaming to succeed. * File and File1 must both be files or directories * In the same drive letter*/ BooleanFlag =File.renameto (FILE1); System.out.println ("Rename return" +flag); /*** File Last Modified date*/System.out.println (NewDate (File1.lastmodified ())); File.list ();//returns a string array, only the name can be obtainedFile.listfiles ();//returns a file array, which gets the file object and the file Operation } Public Static voidcreateFile (String filedirparam, string fileName)throwsIOException {File Filedir=NewFile (Filedirparam); BooleanFiledirflag =filedir.exists (); System.out.println ("Is Filedir exists" +Filedirflag); if(!Filedirflag) { BooleanCreatedirflag =Filedir.mkdirs (); System.out.println ("is create Filedir success" +Createdirflag); } File File=NewFile (Filedirparam +fileName); BooleanFileexistflag =file.exists (); System.out.println ("Is FileExist" +Fileexistflag); if(!Fileexistflag) { BooleanFlag =File.createnewfile (); System.out.println ("Is file created" +flag); } System.out.println ("The partition Size" +file.gettotalspace ()/(1024*1024*1024) + "G"); } Public StaticLong getfilelength (String filedirparam, String fileName)throwsioexception{Long filelength= 0l; InputStream ins=NULL;//input Streamins=NewFileInputStream (NewFile (filedirparam+fileName)); while(Ins.read ()! =-1) {Filelength++; } System.out.println ("File length is" +filelength); Ins.close ();//FileInputStream has buffers, so it must be closed after use, otherwise it may cause memory to be full, data loss returnfilelength; } Public StaticLong getfilelengthnew (String filedirparam, String fileName)throwsioexception{Long filelength= 0l; List<Object> objlist =NewArraylist<object>(); HASHMAP HT; Hashtable HS; Objlist.hashcode (); InputStream ins=NULL;//input Stream byte[] buffer =New byte[1024];//in stack memory is defined as 1024 consecutive byte unitsINS =NewFileInputStream (NewFile (filedirparam+fileName)); while(Ins.read (buffer)! =-1) {} System.out.println ("File length is" +filelength); Ins.close ();//FileInputStream has buffers, so it must be closed after use, otherwise it may cause memory to be full, data loss returnfilelength; } Public Static voidMain (string[] args) {Try{getfilelengthnew ("d:/a/d/e/c/", "test.txt"); } Catch(IOException e) {e.printstacktrace (); } }}
Use of the 1-file class