java read the file using a relative path project environment, the use of read the file using a relative path examples:

* Directory structure:
| ___src
| ___resource
| ___train.txt, test.txt


  String filepath="resource/train.txt";// Note the contents of the filepath;
  File file=new File(filepath);

* We pay attention to filepath content, default to the current user directory ( "user.dir") under, namely: Project root directory "D: \ DecisionTree", it is therefore at this time relative path (to user.dir Path-based path) to "resource / train.txt". This, JVM can be under the "user.dir" and "resource / train.txt" get the full path (ie, the absolute path) "D: \ DecisionTree \ resource \ train.txt", has never been found train.txt file.

* Note: Relative paths start at non-diagonal "/"; For example:
filepath = "resource / train.txt";
Rather than filepath = "/ resource / train.txt"; / / error!

2, javaEE the environment, the use of Classloader to use relative path to read xml example:

* Reads as follows:
java read xml file using a relative path:
A, xml file location for the general there are three:
1. Placed under WEB-INF;
2.xml files in the / WEB-INF/classes directory or classpath of the jar package;
3. Devoted to analysis of its java similar to a package, not necessarily the classpath;

B, corresponding to the two kinds of use relative paths to read method:

Method 1: (not verified)
Xml file in the WEB-INF directory, and then code:

InputStream is=getServletContext().getResourceAsStream( "/WEB-INF/xmlfile.xml" );

Method 2: The xml file in the / WEB-INF/classes directory or classpath of the jar package, you can use the static ClassLoader

Methods getSystemResourceAsStream (String s) to read;

String s_xmlpath="com/spf/web/ext/hotspot/hotspotxml/hotspot.xml";
InputStream in=ClassLoader.getSystemResourceAsStream(s_xmlpath);

Method 3: xml in the random path to a package:

String s_xmlpath="com/spf/web/ext/hotspot/hotspotxml/hotspot.xml";
ClassLoader classLoader=HotspotXmlParser.class.getClassLoader();
InputStream in=classLoader.getResourceAsStream(s_xmlpath);

Original Address:

分类:Java 时间:2010-03-29 人气:791
blog comments powered by Disqus


  • DB2 directory structure 2010-04-21

    - Start 1: DB2 installation directory structure <br /> operating system environment variables DB2DIR (set | grep DB2DIR) specifies the DB2 installation directory, usually: / opt/ibm/db2/V9.5, directory structure is as follows: Quote adm - only the i

  • Java Web Applications - Create. Publishing and directory structure 2010-12-28

    Creating and publishing Web Applications Java Web Applications by a group of static HTML pages, Servlet, JSP and other related class composition. Each component in Web applications have a fixed storage directory. Web application configuration informa

  • Web Development directory structure of file storage 2010-06-12

    Web applications often have file storage needs, how to build the directory structure should we do? Let us do the analysis under For the small number of users to write information, a large number of users are looking at the information site: for examp

  • Android project directory structure 2010-09-20

    We HelloAndroid project, to analyze the project directory structure, further in-depth understanding of Android project. First start Eclipse, launched "Package Explorer" Navigator's "HelloAndroid" project, as shown in Figure 3-1. Figure

  • The fourth series of tutorials Android: Android project directory structure 2010-11-16

    Wrote Two days off at home network, you can not write at night, please read the official forgive me. The last time we used the Android ADT created a HelloWorld project, and describes the configuration of the simulator and the simulator, this section

  • The third series of tutorials Android: Android project directory structure 2010-11-21

    A: Android project directory structure Android Overview of project structure. Actually, Android and the Java project directory structure is similar, not much change, as shown below: Can see the src folder in the source file, Android2.2 is a reference

  • Android Development - Learn about android project directory structure 2010-12-17

    Android directory structure We must first of its profits. Android same when I design a program, but also very necessary to understand the android directory structure, directory structure by understanding that we can know what type of resources placed

  • Project directory structure 2009-08-09

    Subversion (SVN) is a version of management software or servers, can provide hosted, "the source code or documentation," and be able to synchronize files exotic updated version of the difference between records, sub-clear differences have docume

  • Java for beginners JDK command-line environment path problem 2010-03-29

    Screen appears: Exception in thread "main" java.lang.NoClassDefFoundError: C: \ JavaTest \ Hello hello.class obvious why CLASS is not in because in the CLASSPATH 1, SET CLASSPATH =,,,, 2, JAVA-CLASSPATH C: \,,, JDK will be with the spaces C: \ J

  • Grails project directory structure 2010-03-29

    Directory name associated description grails-app This directory contains the core of the application of the Grails artifacts (core artifact) + Conf This directory contains configuration files such as DevelopmentDataSource.groovy + Controllers This di

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 黔ICP备15002463号-1

processed in 0.515 (s). 15 q(s)