java read the file using a relative path
Advertisements
* Directory structure:
DecisionTree
| ___src
| ___com.decisiontree.SamplesReader.java
| ___resource
| ___train.txt, test.txt
* SamplesReader.java:
String filepath="resource/train.txt";// Note the contents of the filepath; File file=new File(filepath); ……
* We pay attention to filepath content, java.io 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;
Code:
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: http://www.blogjava.net/flysky19/articles/93492.html
Related Posts of java read the file using a relative path
-
JavaScript inheritance
About JavaScript inherited a small example ... <! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.01 / / EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" conte ...
-
JavaScript String Functions Guinness
Own JS function 1.Asc (x), Chr (x): convert characters, character code 2. Filter: Search string array of a specific string Format: v = filter (x, s [, include [, compare]]) Examples: Dim x ()={" kjwang "," wangkj "," peter ")
-
EJB ant script to deploy template works
<? xml version = "1.0" encoding = "UTF-8"?> <! - Name Project name basedir build.xml file directory -> <project name="HelloWorld" basedir="."> <! - Property variables -> <! - The sour ...
-
log4j easy application in java
JAVA development, frequently used the log output, in a so-called most of the software company will have its own set of configuration style, re-read the configuration file to initialize property of the log, it will be good, but sometimes may not need to fu
-
java read file
java read documents in two ways: java.io and java.lang.ClassLoader When using the java.io, when java.lang.ClassLoader use it? (Note: If prior to read xml file java read file clearly aware of these two methods have been like! Can take much less to understa
-
What is the appfuse
First, Appfuse brief introduction Matt Raible are Appfuse developed a guiding entry-level J2EE framework, how to integrate its popular Spring, Hibernate, ibatis, struts, Xdcolet, junit, etc. give the basic framework of the model, the latest version 1.7 is












