—-絕對路徑
*注意:相對路徑的起始處無斜杆”/”;
例如:filepath=”resource/train.txt”;
而不是filepath=”/resource/train.txt”; //error!
在Web開發中儘量使用絕對路徑,前一段路徑無論是用的Windows或Linux開發,都可以利用 ServletActionContext.getServletContext().getRealPath(path); 來獲取!
簡單來說就是 路徑的拼接 來組成要讀取文件的絕對路徑
—-相對路徑
使用java project環境,使用java.io用相對路徑讀取文件的例子:
*目錄結構:
DecisionTree
|___src
|___com.decisiontree.SamplesReader.java
|___resource
|___train.txt,test.txt
*SamplesReader.java:
String filepath=”resource/train.txt”;//注意filepath的內容;
File file=new File(filepath);
……
當然在Web開發中相對路徑卻拿不到對應的文件。