1 String projectURL = System.getProperty("user.dir") ;
2 System.out.println("工程所在的路徑地址:"+projectURL);
3
2 System.out.println("工程所在的路徑地址:"+projectURL);
3
輸出結果:
1 工程所在的路徑地址:D:\J2EE\MyEclipse6.0_workspace\mobilenet2.0
簡單說明:
這個JAVA APPLICATION文件是在mobilenet2.0的工程里面,故打印出來的是工程所在路徑地址
剛去csdn的博客看了一下,發現原來已經總結過一次,現在轉過來
1
2
3
1 、利用 System.getProperty() 函數獲取當前路徑:
4
5
System.out.println(System.getProperty("user.dir"));//user.dir 指定了當前的路徑
6
7
8
9
2 、使用 File 提供的函數獲取當前路徑:
10
11
File directory = new File("");// 設定為當前文件夾
12
13
try{
14
15
System.out.println(directory.getCanonicalPath());// 獲取標準的路徑
16
17
System.out.println(directory.getAbsolutePath());// 獲取絕對路徑
18
19
}catch(Exceptin e){}
20
21
22
23
File.getCanonicalPath() 和 File.getAbsolutePath() 大約只是對于 new File(".") 和 new File("..") 兩種路徑有所區別。
24
25
26
27
# 對于 getCanonicalPath() 函數,“ ." 就表示當前的文件夾,而” .. “則表示當前文件夾的上一級文件夾
28
29
# 對于 getAbsolutePath() 函數,則不管” . ”、“ .. ”,返回當前的路徑加上你在 new File() 時設定的路徑
30
31
# 至于 getPath() 函數,得到的只是你在 new File() 時設定的路徑
32
33
34
35
比如當前的路徑為 C:\test :
36
37
File directory = new File("abc");
38
39
directory.getCanonicalPath(); // 得到的是 C:\test\abc
40
41
directory.getAbsolutePath(); // 得到的是 C:\test\abc
42
43
direcotry.getPath(); // 得到的是 abc
44
45
46
47
File directory = new File(".");
48
49
directory.getCanonicalPath(); // 得到的是 C:\test
50
51
directory.getAbsolutePath(); // 得到的是 C:\test\.
52
53
direcotry.getPath(); // 得到的是 .
54
55
56
57
File directory = new File("..");
58
59
directory.getCanonicalPath(); // 得到的是 C:\
60
61
directory.getAbsolutePath(); // 得到的是 C:\test\..
62
63
direcotry.getPath(); // 得到的是 ..
64
65
66
67
關于System.getProperty方法的參數見
68
69
70
71

2

3

4

5

6

7

8

9

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

Life,simple and happy!