Java loading resource file several methods

Source: Internet
Author: User

from:http://andyzhu.blog.51cto.com/4386758/775836/

ImportJava.net.URL; ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;  Public classTestmain { Public Static voidMain (string[] args) {// ########################################################################################################### #         //1: Use the GetResource () method of Class class//finds a resource in the current package (specifies a relative path, and all else returns NULL.) ) URL FILEPATHURL1 = Testmain.class. GetResource ("Beans_samelocation.xml"); //Search for resources at the root (requires file delimiter "/", all other returns NULL.) ) URL filePathUrl2 = Testmain.class. GetResource ("/beans.xml"); //find resources within different packages (Specify relative path (requires file delimiter "/"), and all others return NULL. ) URL filePathUrl3 = Testmain.class. GetResource ("/test/spring/beanpost/file/beans_difflocation.xml"); // ########################################################################################################### #         //2: The GetResource () method of the ClassLoader class using Class class//finds resources within the same package and always returns NULL. //URL filePathUrl3 =//TestMain.class.getClassLoader (). GetResource ("Beans_samelocation.xml"); //finds a resource at the root, specifies a relative path, and all else returns NULL. URL filePathUrl4 = Testmain.class. getClassLoader (). GetResource ("Beans.xml"); //look for resources within different packages, specify relative paths, and all others return NULL. URL filePathUrl5 = Testmain.class. getClassLoader (). GetResource ("Test/spring/beanpost/file/beans_difflocation.xml"); // ########################################################################################################### #         //3: Use the Getsystemresource () method of the ClassLoader class//finds a resource within the specified package, specifies a relative path, and returns null for all others. URL filePathUrl6 = Classloader.getsystemresource ("Test/spring/beanpost/beans_samelocation.xml"); //Ibid .URL filePathUrl7 = Classloader.getsystemclassloader (). GetResource ("Test/spring/beanpost/beans_samelocation.xml"); //in the root search, specify a relative path, and all others return NULL. URL FilePathUrl8 = Classloader.getsystemresource ("Beans.xml"); //Ibid .URL filePathUrl9 = Classloader.getsystemclassloader (). GetResource ("Beans.xml"); // ########################################################################################################### #         //4: Load the resource with thread (recommended this method)//finds resources within the specified package (relative path), and returns null for all others. FilePathUrl6 = Thread.CurrentThread (). Getcontextclassloader (). GetResource ("Test/spring/beanpost/beans_ Samelocation.xml "); //in the root search, (relative path), all other returns NULL. FILEPATHURL7 = Thread.CurrentThread (). Getcontextclassloader (). GetResource ("Beans.xml"); //find resources within different packages (relative paths), others return null. FilePathUrl8 = Thread.CurrentThread (). Getcontextclassloader (). GetResource ("Test/spring/beanpost/file/beans_ Difflocation.xml "); // ########################################################################################################### # System.out.println (Filepathurl1.getfile ());         System.out.println (Filepathurl2.getfile ());         System.out.println (Filepathurl3.getfile ());         System.out.println (Filepathurl4.getfile ());         System.out.println (Filepathurl5.getfile ());         System.out.println (Filepathurl6.getfile ());         System.out.println (Filepathurl7.getfile ());         System.out.println (Filepathurl8.getfile ());         System.out.println (Filepathurl9.getfile ()); System.out.println ("----------------------------------------------------------------------------------------");         System.getproperties (). List (System.out); System.out.println ("----------------------------------------------------------------------------------------"); ApplicationContext AC=NewClasspathxmlapplicationcontext ("Beans.xml"); Animal Animal= (Animal) ac.getbean ("Animal");         System.out.println (Animal.speak ()); Animal.setage (88); Animal animal0= (Animal) ac.getbean ("Animal");          System.out.println (Animal0.speak ()); ApplicationContext AC1=NewClasspathxmlapplicationcontext ("Beans.xml"); Animal Animal1= (Animal) ac1.getbean ("Animal");     System.out.println (Animal1.speak ()); } } 

Java loading resource files in several ways

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.