Whether you are a novice or a veteran, in the program to read the resource files will always encounter some files can not find the problem, which is related to the implementation of Java bottom, can not calculate the bug, as long as the correct method, the problem can be solved. folder structure of the project: Repathtest
├─src
│└─com
│└─lavasoft
│├─test
│└─res
├─doc
1. Using relative paths in the Java Development tools ProjectIn project, the root directory of the relative path is the root folder of project, where the Repathtest folder is. The file is created in the following notation:NewFile ("Src/com/lavasoft/res/a.txt"); File f =NewFile ("Doc/b.txt"); Note: The path does not start with "/", it is wrong to get out of the IDE, and not every IDE, but all I see is this.
2. Read the In-package file via ClasspathRead the file in the package, the path used must be a relative classpath path, such as a, in the package, you can create a byte stream reading: InputStream in = ReadFile.class. getResourceAsStream ("/com/lavasoft/res/a.txt"); with a byte stream, you can read the contents of the file. Note: This must begin with "/";
3. Take a look at the complete test code PackageCom.lavasoft.test;
Importjava.io.*;
/**
* Java reads a file relative to a path
*
* @author leizhimin 2010-1-15 10:59:43
*/
PublicclassReadFile {
PublicStaticvoidMain (string[] args) {
Readtexta_byclasspath ();
Readtexta_byprojectrelativepath ();
Readtextb_byprojectrelativepath ();
}
/**
* Read (in-package) files from the project relative path, noting that "/" does not begin with
*/
PublicStaticvoidReadtexta_byprojectrelativepath () {
System.out.println ("-----------------readtexta_byprojectrelativepath---------------------");
File f =NewFile ("Src/com/lavasoft/res/a.txt");
String a = file2string (f,"GBK");
System.out.println (a);
}
/**
* Read (out-of-package) files by project relative path, note that "/" does not begin with
*/
PublicStaticvoidReadtextb_byprojectrelativepath () {
System.out.println ("-----------------readtextb_byprojectrelativepath---------------------");
File f =NewFile ("Doc/b.txt");
String B = file2string (f,"GBK");
System.out.println (b);
}
/**
* Read the In-package file via Classpath, and note the beginning with "/"
*/
PublicStaticvoidReadtexta_byclasspath () {
System.out.println ("-----------------readtexta_byclasspath---------------------");
InputStream in = ReadFile.class. getResourceAsStream ("/com/lavasoft/res/a.txt");
String a = stream2string (in,"GBK");
System.out.println (a);
}
/**
* Convert file to String
*
* @param f file
* @param the character set of the CharSet file
* @return File contents
*/
PublicStaticString File2string (File F, string charset) {
String result =NULL;
Try{
result = Stream2string (NewFileInputStream (f), CharSet);
}Catch(FileNotFoundException e) {
E.printstacktrace ();
}
returnResult
}
/**
* Convert file to String
*
* @param in Byte stream
* @param the character set of the CharSet file
* @return File contents
*/
PublicStaticString stream2string (InputStream in, string charset) {
StringBuffer SB =NewStringBuffer ();
Try{
Reader r =NewInputStreamReader (in, CharSet);
intlength = 0;
for(Char[] C =NewChar[1024]; (length = R.read (c))! =-1;) {
Sb.append (c, 0, length);
}
R.close ();
}Catch(Unsupportedencodingexception e) {
E.printstacktrace ();
}Catch(FileNotFoundException e) {
E.printstacktrace ();
}Catch(IOException e) {
E.printstacktrace ();
}
returnSb.tostring ();
}
} (the code is coarse, the exception is not taken seriously) operation Result:-----------------Readtexta_byclasspath---------------------
Aaaaaaaaa
Sssssssss
-----------------Readtexta_byprojectrelativepath---------------------
Aaaaaaaaa
Sssssssss
-----------------Readtextb_byprojectrelativepath---------------------
bbbbbbbbbbb
Process finished with exit code 0 This is done with the idea development tool, and the result is no problem, and if you switch to console execution, the Read method with the project's relative path will fail because it is out of the project's development environment at this point----- This problem often bothers some rookie, the Code in the development tool good, post-release execution failed! I'll cut a picture below:
5. Get the absolute path of the file under ClasspathAn absolute path is required when writing to a file using a relative path. Here's an example: PackageCom.lavasoft;
ImportJava.io.File;
/**
* Classpath file Absolute path acquisition test
*
* @author leizhimin 2010-1-18 9:33:02
*/
PublicclassTest {
file path for//classpath
PrivateStaticString CP ="/com/lavasoft/cfg/syscfg.properties";
PublicStaticvoidMain (string[] args) {
//absolute path of the current class
System.out.println (Test.class. GetResource ("/"). GetFile ());
//Specify an absolute path to the Classpath file
System.out.println (Test.class. getresource (CP). GetFile ());
//Specify an absolute path to the Classpath file
File f =NewFile (Test.class. getresource (CP). GetFile ());
System.out.println (F.getpath ());
}
} Output:/d:/projects/bbt/code/cdn/planrpt/out/production/planrpt/
/d:/projects/bbt/code/cdn/planrpt/out/production/planrpt/com/lavasoft/cfg/syscfg.properties
D:\projects\bbt\code\cdn\planrpt\out\production\planrpt\com\lavasoft\cfg\syscfg.properties
Process finished with exit code 0
SummaryThe use of engineering relative paths is unreliable. Using the classpath path is reliable. For the files to be read by the program, as far as possible under the classpath, so as to ensure that the development and release of the normal read. -----------------------Recommended Resources: http://www.91ziyuan.com/Html/?904.htmlhttp://shirlly.javaeye.com/blog/218499
This article is from the lava blog, so be sure to keep this source http://lavasoft.blog.51cto.com/62575/265821
Java relative path Read file