Use of the 1-file class

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.