The constants and Paths of Java--io

Source: Internet
Author: User

Package Com.io.demo;import Java.io.File;/** Two common constants * path separators; File.pathseparator * Name separator under Windows: \ Non windows:/* * relative path and absolute path construct file Object **/ Public classIofile { Public Static voidMain (string[] args) {System. out. println (File.pathseparator);// ;System. out. println (File.separator);//          //path representation 1, because of the escape character, use a double slashString Path ="E:\\soft\\book\\1.txt"; //path Representation 2, can do cross-platformPath="E"+file.separator+"Soft"+file.separator+" Book"+file.separator+"1.txt"; //path representation 3, recommendedPath="E:/soft/book/1.txt"; String Parentpath="e:/xp/test"; String name="2.jpg"; //relative PathFile src =NewFile (parentpath,name); SRC=NewFile (NewFile (Parentpath), name); System. out. println (Src.getname ());//2.jpgSystem. out. println (Src.getpath ());//E:\xp\test\2.jpg//Absolute Pathsrc =NewFile ("e:/xp/test/2.jpg"); System. out. println (Src.getname ());//2.jpgSystem. out. println (Src.getpath ());//E:\xp\test\2.jpg//no drive letter, built with User.dirsrc =NewFile ("Test.txt"); System. out. println (Src.getname ());//Test.txtSystem. out. println (Src.getpath ());//Test.txtSystem. out. println (Src.getabsolutepath ());//D:\MyEclipse 10\java300\test.txt    }    }

Several common ways to get filenames:

GetName (): Return name

GetPath (): Returns the full path if it is an absolute path, otherwise the relative path

GetAbsolutePath (): Returns the absolute path

GetParent (): Returns the previous level of the directory, if it is relative, returns null

Method of judging information:

Exists (): File exists, Boolean

Canwriter (): Whether the file is writable, Boolean

CanRead (): Whether the file is readable

Isfile (): is a file or a folder, there is no default folder

Isdirectory (): Whether it is a directory

Isabsolute (): is an absolute path

Judging length:

Length (): Number of bytes, if the folder length is 0, cannot read, only the length of the file can be read

To create and delete files:

CreateNewFile (): Create file Boolean type

Delete (): Delete file

The constants and Paths of Java--io

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.