Java路径问题解决方案(Java path problem solution)Java path problem solutions are collected [rotate]1, TestURL (),.Class.getResource ("),.GetPath (), or TestURL (),.Class.getResource (") ".GetFile" (), the path obtained, cannot be directly applied by FileReader () and FileWriter ().The reason is that the URL of space, special characters (%, # [], etc.) and Chinese of encoding processing.For example, the space becomes%20.There is a solution (1). After using repaceAll ("%20", "\"), only the space problem can be resolved. But the path contains% and Chinese is no good.There is a solution to (2), URLDecoder.decode (STR, UTF-8) decoding, but only part of the solution, if the path contains +, is not going to solve, because URL is not complete with URLEncoder.encode (STR, UTF-8) encoding, + is decoded, turned into space.Method (3) can solve all problems with TestURL(),.Class.getResource ("),".ToURI "(),".GetPath "(), but need to deal with" URISyntaxException "exception, which is more troublesome.The space problem in the Java pathIf spaces are in the path, then1. uri.getpath (). The space in the returned path still appears as "space", such as /F:/MyEclipse Workspace/project/bin/...In addition, all the spaces in the path returned by URL appear in the form of "%20", and uri.toString () appears in the form of "%20".2. new File (String filePath); parameter accepts correct URI format and the "space" (20%) the correct relative / absolute string path, or even to the path is correct will appear in the file not found exception.The path separators returned by 3. URL/URI are all "/", and the path separators returned by File are "\"". The existing file path string returns the space in the "space", and there is no path from new file (getPath), return to the path of the space is still new File (String filePath) the original form of the parameters, i.e. filePath (getPath) is a space space is returned that is "%20" or "%20".4.new URL (); the arguments can be either the right URI or a string in the URI format; if the string is incomplete URI, the creation fails.5.File.toURI (file) will be the path name in the "space" into "%20", and then add protocol: "file:/" in the path, and (File.toURL) only in the file path before simply add protocol: "file:/" instead of "space" into "%20", regardless of the original is the "space" or "%20" will only be preserved!6.Woden in WSDLReader.readWSDL (String s) to achieve theparameter s into URL, so string parameter s must not have spaces, should be replaced by 20%. The parameter s is best a string in the standard URI format.Java path resolutionThe path problem of Java is relatively complicated. Recent work involves the creation and reading of files, and the problems encountered in actual use are summarized below:I. interpretation of relative paths1. relative paths, i.e., relative paths relative to the current user directory, can be obtained either by the general java project or the web projectString relativelyPath=System.getProperty ("user.dir");For the general java file in the project is relative to the root directory of the project, and for the web project file path, it may be a path to a server, at the same time, different web servers are different (Tomcat is relative to the Tomcat installation directory \bin). For this reason, personally, it is better not to use "relative paths" relative to the current user directory in the web project". By default, however, the class in the java.io package always analyzes the relative path name based on the current user directory. This directory is user. by the system propertyDir specifies the directory that is typically called by the Java virtual machine. That is to say, it is better not to use therelative path when using a class in the java.io package. Otherwise, although in the SE program may be normal, but in the EE program, do not good, it will bring a problem oh.2. relative path relative to classpathSuch as the relative path of the path relative tofile:/D:/mywork/javaprj/MyTest/bin. Among them, bin is the classpath of this project. All the Java source files, compiled.Class files, are copied into this directory.The two type loads the directory (i.e., when it runs a class, gets its load directory)1., whether it's a general java project or a web project, first locate the first level directory where you can see the package pathInputStream is=ReadWrite.class.getClassLoader().GetResourceAsStream ("DeviceNO");The path to the DeviceNO file is the project name \src\DeviceNO; the first directory of the package in class ReadWrite is located under the SRC directory.2. and 1 are similar, the difference is this method must havea leading '/'InputStream is=ReadWrite.class.getResourceAsStream ("DeviceNO");The path to the DeviceNO file is the project name \src\DeviceNO; the first directory of the package in class ReadWrite is located under the SRC directory.Three. Web project root directory1., you can create a servlet and write the following statement in its init methodServletContext, sc=this.getServletContext ();String temp=sc.getRealPath ("/");The output path obtained similar results:"D:\Apache\Tomcat6.0\webapps\windpower\" (windpower project name), if it is called s1.getRealPath ("") is the output of "D:\Apache\Tomcat6.0\webapps\windpower" (note that in the last one less "\")2. in httpServletRequest, you can pass the following statementString cp=request.getSession ().GetServletContext().GetRealPath ("/"); the resulting output path is similar: "D:\Apache\Tomcat6.0\webapps\windpower\""Four. Class path (classpath) access (in Eclipse/MyEclipse, the path to the SRC or classes directory)Method 1., Thread.currentThread (),.GetContextClassLoader (),.GetResource ('),.GetPath ()Such as:String, path=Thread.currentThread (),.GetContextClassLoader (),.GetResource ("),.GetPath ();System.out.println (path);Print: /D:/windpower/WebRoot/WEB-INF/classes/"Method 2., ParsingXML.class.getClassLoader (),.GetResource ("),".GetPath "() (ParsingXML is SRC, class in a package, same below)Such as:String, path=ParsingXML.class.getClassLoader (),.GetResource ("),.GetPath ();System.out.println ("ParsingXML.class.getClassLoader ()).GetResource--" +path ");Print: ParsingXML.class.getClassLoader().GetResource--/D:/windpower/WebRoot/WEB-INF/classes/"In addition, if you want to put a file in a package, you can get the directory where the file is located, that is, to locate the last directory of the package.ParsingXML.class.getResource ("").GetPath ();Such as:String path=ParsingXML.class.getResource ("").GetPath ();System.out.println ("ParsingXML.class.getResource---" +p2);Print:"ParsingXML.class.getResource---/D:/windpower/WebRoot/WEB-I NF/classes/parsing/" (ParsingXML is the class in the parsing package under the SRC directory)Five. Property file read:Method 1.Static {PS = new, Properties ();{tryInputStream in = ReadWrite.class.getResourceAsStream ("DeviceNO");Ps.load (in);In.close ();} catch (Exception, e) {E.printStackTrace ();Ps.getProperty ("key")Method 2.Locale locale = Locale.getDefault ();ResourceBundle localResource = ResourceBundle.getBundle ("windpower/DeviceNOProperties", locale);String value = localResource.getString ("1");System.out.println ("DeviceNO:" + value);In the project SRC directory, the file DeviceNOProperties.properties (name suffix must be properties) reads as follows: 1=3 output results as follows: "DeviceNO:3""Six. Code conversion problem:GetResource ClassLoader UTF-8 was used for the encoding of path information, when there is Chinese and spaces in the path, he will convert to these characters, this is often not the true path, we want to get this, call the URLDecoder decode method to decode, in order to get Chinese spaces and the original pathFor example: the result isfile:/C:/Documents%20and%20Settings/%e5%ba%84%e6%99%93%e6%a f%85/Local%20Settings/Temp/temp0.jar! /db/dmozdata.mdb!And we expect the C:/Documents path, P, source, and so on. Here, we just want to return the value decode before we get to the path. Use UTF-8 encoding. Java code:String, configPath = this.getClass (),.GetClassLoader (),.GetResource (` allowPath.xml '),.GetFile ();ConfigPath = .URLDecoder.decode (configPath, UTF-8);In addition Java URL encoding and decoding functions of .URLEncoder.encode (String s) and.URLDecoder.decode (String s); encoding and decoding function escape in JavaScript URL (String s) and unescape (String s);Seven. Summary:When using relative paths, we should use relative paths relative to the current classpath.ClassLoader class getResource (String, name), getResourceAsStream (String, name) and other methods, using the relative path of the classpath relative to the current project to find resources.The same is true of the getBundle (String path) of the ResourceBundle class that is commonly used to read property files.By looking at the source code of the ClassLoader class and its associated classes, it is actually using the absolute path of the URI form. By getting the absolute path of the current classpath's URI form, the absolute path of the URI form of the relative path is constructed.。