Java Read file Best practices

Source: Internet
Author: User

    1. 1. Preface

Java application is a very common problem, how to read the Jar/war package and the path of the configuration file, different people according to different practices summed up the different scenarios, but others application will be due to the environment and other differences to find a variety of problems, this article from the principle of interpretation of best practice.

    1. 2. Reference Scheme

2.1.log4j

The code for the log4j read configuration is:

Propertyconfigurator.configure ("Log4j.properties");

The actual file reads performed are:

FileInputStream istream = new FileInputStream (configfilename);

That is, the execution environment ${pwd} finds the file, which is the same path as the new file ().

2.2.Hibernate

The code for Hibernate read configuration is:

Configuration configuration = new configuration (). Configure ("Hibernate.cfg.xml");

The actual file reads performed are:

String stripped = Resource.startswith ("/")? Resource.substring (1): resource;

ClassLoader ClassLoader = Thread.CurrentThread (). Getcontextclassloader ();

InputStream stream = Classloader.getresourceasstream (stripped);

is to find the file through the relative path of the ClassLoader, which has the advantage of being able to read the files in the jar and that the new file () cannot be impersonated.

    1. 3. Programme Analysis

3.1. Scenario Comparison

In relative terms, hibernate's file read is more practical, mainly because it can read ordinary files, but also can read the files in the Jar/war, relatively more general.

3.2. Reference Documents

The more common scenario is to use Class.getresourceasstream (String name), Actually eventually call Classloader.getsystemresourceasstream, but a lot of people do not fully understand the getresourceasstream, so it is doubtful sometimes need to add "/" on the path, sometimes not.

In short, the base path of ClassLoader is fixed, and the base path of class is not the same, so the classloader are relative paths, and class reads have both absolute path and relative path.

There are explanations in the Class.getresourceasstream documentation:

(1) If name begins with "/", it reads the file under ${pwd}. Files such as Hibernate.cfg.xml are applied with the same level path as the Read jar.

(2) If Name does not start with "/", it reads the file under the package of the path where the class is located. Apply to read files related to class such as *.hbm.xml.

    1. 4. Practical Application

4.1. Best Practices

Since Class.getresourceasstream will continue to invoke the ClassLoader method, establishing a singleton ClassLoader is the best solution, such as the implementation of hibernate.

In combination with Maven, the files in the src/main/resources are read using Classloader.getsystemresourceasstream ("Log4j.properties"), for *. Hbm.xml such as ORM mapping files, etc., need to add a package path, such as "Com/zheezes/*.hbm.xml".

4.2. Best Practices

However, because the getclass () approach is more concise and the performance penalty is very small, it is more commonly used in practice to call GetClass (). getResourceAsStream ().

In combination with Maven, the files in the src/main/resources are read using the absolute path GetClass (). getResourceAsStream ("/log4j.properties"), for *. ORM mapping files such as hbm.xml are generally read directly in the PO class using the relative path "*.hbm.xml".

Java Read file Best practices

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.