Java Basic---> File---> File storage path: Absolute path and relative path

Source: Internet
Author: User

Content Overview:

In the actual project, sometimes you need to store some files, such as storing some pictures, data files, and so on, to consider where to store these files.

There are usually two ways to get the file storage path:

Method One, obtain the system effective disk such as d:/, and then the path extension, the extended path as the root path to store these files. One drawback of this approach is that it can be cumbersome to migrate programs in different operating systems.

Method Two, get the current folder (that is, the program Engineering directory), such as D:/projectname, and then the path extension, the extended path as the root path to store these files. The directory in which the program files are eventually stored and the program code directories are in the same folder. This method is a more commonly used method, the program written using this method is easier to migrate in different operating systems, but also easy to manage the program, such as when the program uninstall, delete the entire program folder can ensure that the program data is also cleared, and the use of "method one" program is not guaranteed to delete the program data files, Requires the user to manually find the program's data file storage path, and then manually deleted.

The following code illustrates the Java programming implementation of the two methods described above:

/** * @authorLXRM * @date 20161115 * @description different computer system drive letter is not the same, some computers have "C:" "D:" "E:" "F:" * But some computers have only C and D drives, and Lin system drive letter in UX system is/home etc * This program gives an example of how to obtain a valid drive letter from the local computer **/ Packagefile_operation;ImportJava.io.File;Importjava.io.IOException; Public classExample8_geteffectivedisks { Public Static voidMain (string[] args) {/*first, get the native valid drive letter, C:d: E: ... * This is the method of obtaining an absolutely available path (not commonly used), generally using the "two" described in the method of obtaining a relative path to hold the data, files.*/file[] Roots=file.listroots (); System.out.println ("Valid disk characters for the local computer are:");  for(inti=0;i<roots.length;i++) {System.out.println (Roots[i].getabsolutepath ()); } String RootPath=roots[1].getabsolutepath ();//Select the last disk in the list above as the root directory to use belowString path=rootpath+ "/leapmotiondata/framedata";//Storage directory for frame object dataSystem.out.println ("LeapMotion sampled data will be placed in the following directory: \ n" +path); File dir=NewFile (path); if(!dir.exists ())        {Dir.mkdirs (); } System.out.println (Dir.getabsolutepath ()+ "Is there:" +dir.exists ());/*second, get the current folder * This is a way to get a relative path (commonly used), easy for the program to migrate in different computer OS * Get the folder where the current project is located, as in this case D:\workspaceOfJavaEclipse\javaTest*/File Root2=NewFile (".");//get current folder (i.e. project directory), result D:\workspaceOfJavaEclipse\javaTest//file Root2=new file ("..");//gets the parent folder of the current folder, resulting in D:\workspaceOfJavaEclipse        Try{String RootDir2=Root2.getcanonicalpath (); System.out.println ("Current project folder:" +RootDir2); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }//End Main}

Java Basic---> File---> File storage path: Absolute path and relative path

Related Article

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.