[Excerpt] How to Use relative paths in Java

Source: Internet
Author: User
Tags file separator
How to use relative paths

--- From Internet

InProgramData Files or resource files (e.g.) are often used. If an absolute path is used in a program, the program will lose flexibility. For example, our application directory is D: \ NECs, and the data directory is D: \ NECs \ database. If we use the data file, use the D: \ NECs \ database directory name. When our program is moved to drive C, an error occurs because the data directory is changed to c: \ NECs \ database. Therefore, we need to use the relative directory \ database.

We can use the java. util. properties class, which is a hashtable and stores many system attributes. Where:

......
Java. Home Java installation directory
Java. Class. Path Java class path
File. Separator file separator ("/" on UNIX)
User. dir user's current working directory
......

We can use the java. Lang. System Method getproperty (string key) to obtain the required properties.

Curdir = system. getproperty ("user. dir ");
Filebar = system. getproperty ("file. separator ");

The data directory in the above example can be expressed as curdir + filebar + "Database"

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.