1、在Java開發工具的project中使用相對路徑
在project中,相對路徑的根目錄是project的根文件夾,在此就是repathtest文件夾了。
創建文件的寫法是:
[Java]
純文本查看 復制代碼
|
1
|
File f =
new
File(
"src/com/lavasoft/res/a.txt"
);
|
[Java]
純文本查看 復制代碼
|
1
|
File f =
new
File(
"doc/b.txt"
);
|
注意:
路徑不以“/”開頭;
脫離了IDE環境,這個寫法就是錯誤的,也並非每個IDE都如此,但我見到的都是這樣的。
2、通過CLASSPATH讀取包內文件
讀取包內文件,使用的路徑一定是相對的classpath路徑,比如a,位於包內,此時可以創建讀取a的字節流:
[Java]
純文本查看 復制代碼
|
1
|
InputStream in = ReadFile.
class
.getResourceAsStream(
"/com/lavasoft/res/a.txt"
);
|
有了字節流,就能讀取到文件內容了。
注意:
這里必須以“/”開頭;
3、看看完整的測試代碼:
[Java]
純文本查看 復制代碼
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
ackage com.lavasoft.test;
import
java.io.*;
/**
* Java讀取相對路徑的文件
*
*/
public
class
ReadFile {
public
static
void
main(String[] args) {
readTextA_ByClassPath();
readTextA_ByProjectRelativePath();
readTextB_ByProjectRelativePath();
}
/**
* 通過工程相對路徑讀取(包內)文件,注意不以“/”開頭
*/
public
static
void
readTextA_ByProjectRelativePath() {
System.out.println(
"-----------------readTextA_ByProjectRelativePath---------------------"
);
File f =
new
File(
"src/com/lavasoft/res/a.txt"
);
String a = file2String(f,
"GBK"
);
System.out.println(a);
}
/**
* 通過工程相對路徑讀取(包外)文件,注意不以“/”開頭
*/
public
static
void
readTextB_ByProjectRelativePath() {
System.out.println(
"-----------------readTextB_ByProjectRelativePath---------------------"
);
File f =
new
File(
"doc/b.txt"
);
String b = file2String(f,
"GBK"
);
System.out.println(b);
}
/**
* 通過CLASSPATH讀取包內文件,注意以“/”開頭
*/
public
static
void
readTextA_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);
}
/**
* 文件轉換為字符串
*
* @param f 文件
* @param charset 文件的字符集
* @return 文件內容
*/
public
static
String file2String(File f, String charset) {
String result =
null
;
try
{
result = stream2String(
new
FileInputStream(f), charset);
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
return
result;
}
/**
* 文件轉換為字符串
*
* @param in 字節流
* @param charset 文件的字符集
* @return 文件內容
*/
public
static
String stream2String(InputStream in, String charset) {
StringBuffer sb =
new
StringBuffer();
try
{
Reader r =
new
InputStreamReader(in, charset);
int
length =
0
;
for
(
char
[] c =
new
char
[
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();
}
return
sb.toString();
}
}
|
運行結果:
[Java]
純文本查看 復制代碼
|
01
02
03
04
05
06
07
08
09
10
|
-----------------readTextA_ByClassPath---------------------
aaaaaaaaa
sssssssss
-----------------readTextA_ByProjectRelativePath---------------------
aaaaaaaaa
sssssssss
-----------------readTextB_ByProjectRelativePath---------------------
bbbbbbbbbbb
Process finished with exit code
0
|
這是通過IDEA開發工具運行的,結果沒問題,如果換成控制台執行,那么使用了項目相對路徑的讀取方式會失敗,原因是,此時已經脫離了項目的開發環境,-----這個問題常常困擾着一些人,代碼在開發工具好好的,發布后執行就失敗了!
5、獲取CLASSPATH下文件的絕對路徑
當使用相對路徑寫入文件時候,就需要用到絕對路徑。下面是個例子:
[Java]
純文本查看 復制代碼
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package
com.lavasoft;
import
java.io.File;
/**
* CLASSPATH文件的絕對路徑獲取測試
*
* @author leizhimin 2010-1-18 9:33:02
*/
public
class
Test {
//classpath的文件路徑
private
static
String cp =
"/com/lavasoft/cfg/syscfg.properties"
;
public
static
void
main(String[] args) {
//當前類的絕對路徑
System.out.println(Test.
class
.getResource(
"/"
).getFile());
//指定CLASSPATH文件的絕對路徑
System.out.println(Test.
class
.getResource(cp).getFile());
//指定CLASSPATH文件的絕對路徑
File f =
new
File(Test.
class
.getResource(cp).getFile());
System.out.println(f.getPath());
}
}
|
輸出:
[Java]
純文本查看 復制代碼
|
1
2
3
4
5
|
/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
|
總結
使用工程相對路徑是靠不住的。
使用CLASSPATH路徑是可靠的。
對於程序要讀取的文件,盡可能放到CLASSPATH下,這樣就能保證在開發和發布時候均正常讀取
更多免費技術資料可關注:annalin1203
