Path problems when reading external files in Java programs

Source: Internet
Author: User

Transferred from: https://www.cnblogs.com/wt20/p/8320346.html

Projects often read some configuration files, so the GetResource method can play an important role

There are two main methods used, one is the bytecode file class class, the other is the ClassLoader class loader

There are two ways to use class classes:

1. Use "/" to get to the classpath path

2. Do not use "/" This is the relative path

ClassLoader class

Without the "/" notation, the classpath path is obtained.

Test code

public class Getresourcetest {public    static void Main (string[] args) {        System.out.println ( GetResourceTest.class.getResource ("/test.properties"). GetPath ());        System.out.println (GetResourceTest.class.getResource ("Test1.properties"). GetPath ());        System.out.println (GetResourceTest.class.getClassLoader (). GetResource ("Test.properties"). GetPath ());        System.out.println (GetResourceTest.class.getClassLoader (). GetResource ("/"));}    }

Results:

/e:/ideajava/studytest/out/production/studytest/test.properties
/e:/ideajava/studytest/out/production/studytest/com/waston/test/test1.properties
/e:/ideajava/studytest/out/production/studytest/test.properties
Null

Project Package Structure

The above is written by https://www.cnblogs.com/wt20/p/8320346.html Bloggers

Recent projects require processing of the database's historical data, and because of the lack of familiarity with the timing tasks of the Oracle database, a timed task is written using Java

In the program need to read a CTL file, but, if given a directory, to the client run time may be due to the file location error can not read to the file, try to use the virtual path, no success, search the Internet using this method can get to the directory, try to use, success.

Path problems when reading external files in Java programs

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.