一.Java文件獲取路徑方式:
各種獲取方式如示例代碼所示:
- package first.second;
- import java.io.File;
- public class GetPath {
- public static void getPath()
- {
- //方式一
- System.out.println(System.getProperty("user.dir"));
- //方式二
- File directory = new File("");//設定爲當前文件夾
- try{
- System.out.println(directory.getCanonicalPath());//獲取標準的路徑
- System.out.println(directory.getAbsolutePath());//獲取絕對路徑
- }catch(Exception e)
- {
- e.printStackTrace();
- }
- //方式三
- System.out.println(GetPath.class.getResource("/"));
- System.out.println(GetPath.class.getResource(""));
- //方式4
- System.out.println(GetPath.class.getClassLoader().getResource(""));
- System.out.println(GetPath.class.getClassLoader().getResource("source.xml"));
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- GetPath.getPath();
- }
- }
其在Java project項目下輸出如下:
- 方式一
- D:\eclipse\juno_workspace\Test
- 方式二
- D:\eclipse\juno_workspace\Test
- D:\eclipse\juno_workspace\Test
- 方式三
- file:/D:/eclipse/juno_workspace/Test/bin/
- file:/D:/eclipse/juno_workspace/Test/bin/first/second/
- 方式四
- file:/D:/eclipse/juno_workspace/Test/bin/
- file:/D:/eclipse/juno_workspace/Test/bin/source.xml
在Web project輸出如下:
- 方式一
- D:\apache-tomcat-6.0.36\bin
- 方式二
- D:\apache-tomcat-6.0.36\bin
- D:\apache-tomcat-6.0.36\bin
- 方式三
- file:/D:/apache-tomcat-6.0.36/webapps/ConsisPro/WEB-INF/classes/
- file:/D:/apache-tomcat-6.0.36/webapps/ConsisPro/WEB-INF/classes/first/second/
- 方式四
- file:/D:/apache-tomcat-6.0.36/webapps/ConsisPro/WEB-INF/classes/
- file:/D:/apache-tomcat-6.0.36/webapps/ConsisPro/WEB-INF/classes/source.xml
說明一下,實驗類是放在first.second包下的,source.xml是src文件下的一個文件。在java project測試的項目名是Test,在web project測試的項目名是ConsisPro,還有就是 web project項目測試時運行在tomcat中,通過調用實驗類GetPath產生的運行結果。
從上面我們可以看出方式一和方式二在java project項目下是獲取的項目根目錄,而在web project項目下獲取的是tomcat的bin目錄。
方式三當使用“/”時獲取的是類編譯後所在的目錄,當不使用“/”是獲取的是編譯好類所在的目錄,也就是前面加“/”產生的目錄加上相應的包目錄。
方式四中第一個獲取的也是類編譯後的目錄,第二個獲取的是也就是我們放在src目錄底下的文件,編譯後放在類編譯後的目錄底下,當我們需要獲取放在Src目錄底下的文件時,用這個方式很方便。二.獲取文件的一些具體情況下的方式
1.獲取你存放在src目錄下的文件,一般爲配置文件,如下圖1所示。推薦使用方式四的中的第二個方式,示例代碼如下:
圖1
- /* @param name 文件名 不包含路徑
- */
- public static String getSrcPath(String name)
- {
- String result=null;
- URL urlpath = GetPath.class.getClassLoader().getResource(name);
- String path=urlpath.toString();
- //remove the head "file:",if it exists
- if(path.startsWith("file"))
- {
- path=path.substring(5);
- }
- path.replace("/", File.separator);
- result=path;
- return result;
- }
2.在java project獲取與src文件夾並列的文件下的文件,如下圖2所示。這種情況推薦使用方式一和方式二,下面以第一種方式給出示例代碼:
圖2
- // filename 文件名 不包含路徑
- // ...args 文件夾名 可以輸入多個文件夾名參數
- public static String getParallelPath(String filename,String ...args)
- {
- String pre=System.getProperty("user.dir");
- String path=pre;
- for(String arg:args)
- {
- path+=File.separator+arg;
- }
- path+=File.separator+filename;
- if(path.startsWith("file"))
- {
- path=path.substring(5);
- }
- path.replace("/", File.separator);
- return path;
- }
3.如果希望無論在java project或者web project項目使用類包底下的一些文件,如下圖3所示。這種情況可以使用方式三中的第二種方式,使用示例代碼如下:
圖3
- public static String getPackagePath(String filename)
- {
- String result=null;
- URL urlpath=GetPath.class.getResource("");
- String path=urlpath.toString();
- if(path.startsWith("file"))
- {
- path=path.substring(5);
- }
- path.replace("/", File.separator);
- result=path+filename;
- return result;
- }
Tips:這個方法要在和要獲取的文件在同一個包中纔可以,在不同包中,需要使用其它方式
4.當在web project項目下想獲取WebRoot目錄底下自定義目錄文件下的文件,如下圖4所示。這種情況推薦方式三和方式四,進行一些操作,下面是一個示例代碼:
圖4
- //獲取WebRoot目錄
- public static String getWebRootPath()
- {
- URL urlpath=GetPath.class.getResource("");
- String path=urlpath.toString();
- if(path.startsWith("file"))
- {
- path=path.substring(5);
- }
- if(path.indexOf("WEB-INF")>0)
- {
- path=path.substring(0,path.indexOf("WEB-INF")-1);
- }
- path.replace("/", File.separator);
- return path;
- }
- //webroot WebRoot目錄
- //filename 文件名
- //...args 文件名所在文件夾,多個參數輸入
- public static String getWebRootFilepath(String webroot,String filename,String ...args)
- {
- String pre=webroot;
- String path=pre;
- for(String arg:args)
- {
- path+=File.separator+arg;
- }
- path+=File.separator+filename;
- if(path.startsWith("file"))
- {
- path=path.substring(5);
- }
- path.replace("/", File.separator);
- return path;
- }
三.空格問題
可以用replaceAll("%20"," "); 來解決部分問題,遇到其它問題可以參考下面的文章http://www.2cto.com/kf/201108/100533.html。我認爲當你得項目設爲UTF-8的格式應該不會遇到空格問題。
參考文獻
1. JAVA中獲取路徑的各種方式。http://gjt1244.blog.163.com/blog/static/19165205620118724046617/
2. Java獲取當前文件路徑。http://www.cnblogs.com/lostyue/archive/2011/06/27/2091686.html
3. JAVA徹底解決獲取空格路徑問題。http://www.2cto.com/kf/201108/100533.html
附錄源代碼
- package first.second;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- public class GetPath {
- public static void getPath()
- {
- //方式一
- System.out.println("方式一");
- System.out.println(System.getProperty("user.dir"));
- //方式二
- System.out.println("方式二");
- File directory = new File("");//設定爲當前文件夾
- try{
- System.out.println(directory.getCanonicalPath());//獲取標準的路徑
- System.out.println(directory.getAbsolutePath());//獲取絕對路徑
- }catch(Exception e)
- {
- e.printStackTrace();
- }
- //方式三
- System.out.println("方式三");
- System.out.println(GetPath.class.getResource("/"));
- System.out.println(GetPath.class.getResource(""));
- //方式4
- System.out.println("方式四");
- System.out.println(GetPath.class.getClassLoader().getResource(""));
- System.out.println(GetPath.class.getClassLoader().getResource("source.xml"));
- }
- /**
- * @param name 文件名 不包含路徑
- */
- public static String getSrcPath(String name)
- {
- String result=null;
- URL urlpath = GetPath.class.getClassLoader().getResource(name);
- String path=urlpath.toString();
- //remove the head "file:",if it exists
- if(path.startsWith("file"))
- {
- path=path.substring(5);
- }
- path.replace("/", File.separator);
- result=path;
- return result;
- }
- // filename 文件名 不包含路徑
- // ...args 文件夾名 可以輸入多個文件夾名參數
- public static String getParallelPath(String filename,String ...args)
- {
- String pre=System.getProperty("user.dir");
- String path=pre;
- for(String arg:args)
- {
- path+=File.separator+arg;
- }
- path+=File.separator+filename;
- if(path.startsWith("file"))
- {
- path=path.substring(5);
- }
- path.replace("/", File.separator);
- return path;
- }
- public static void readFile(String filepath)
- {
- File file=new File(filepath);
- try {
- InputStreamReader sr=new InputStreamReader(new FileInputStream(file));
- BufferedReader br=new BufferedReader(sr);
- String line=null;
- while((line=br.readLine())!=null)
- {
- System.out.println(line);
- }
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static String getPackagePath(String filename)
- {
- String result=null;
- URL urlpath=GetPath.class.getResource("");
- String path=urlpath.toString();
- if(path.startsWith("file"))
- {
- path=path.substring(5);
- }
- path.replace("/", File.separator);
- result=path+filename;
- return result;
- }
- //獲取WebRoot目錄
- public static String getWebRootPath()
- {
- URL urlpath=GetPath.class.getResource("");
- String path=urlpath.toString();
- if(path.startsWith("file"))
- {
- path=path.substring(5);
- }
- if(path.indexOf("WEB-INF")>0)
- {
- path=path.substring(0,path.indexOf("WEB-INF")-1);
- }
- path.replace("/", File.separator);
- return path;
- }
- //webroot WebRoot目錄
- //filename 文件名
- //...args 文件名所在文件夾,多個參數輸入
- public static String getWebRootFilepath(String webroot,String filename,String ...args)
- {
- String pre=webroot;
- String path=pre;
- for(String arg:args)
- {
- path+=File.separator+arg;
- }
- path+=File.separator+filename;
- if(path.startsWith("file"))
- {
- path=path.substring(5);
- }
- path.replace("/", File.separator);
- return path;
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- // GetPath.getPath();
- // //1 test
- // System.out.println(GetPath.getSrcPath("source.xml"));
- // GetPath.readFile(GetPath.getSrcPath("source.xml"));
- // //2 test
- System.out.println(GetPath.getParallelPath("source.xml", "my file"));
- GetPath.readFile(GetPath.getParallelPath("source.xml", "my file"));
- // //3 test
- System.out.println(GetPath.getPackagePath("source.xml"));
- GetPath.readFile(GetPath.getPackagePath("source.xml"));
- }
- }