1. 通用定位到用户目录下:
1 String userDir = System.getProperty("user.dir");
2. web项目定位到WEB-INF/class 目录下:
String userDir = ClassLoader.getSystemClassLoader().getResource("").getPath();
3. 在JSP 中获取路径
a. 得到包含工程名的当前页面全路径:request.getRequestURI() ;
b. 得到工程名:request.getContextPath() ;
c. 得到当前页面所在目录下全名称:request.getServletPath() ;
d. 得到页面所在服务器的全路径:application.getRealPath("页面.jsp") ;
4. 在Servlet中取得路径
a. 得到工程目录:request.getSession().getServletContext().getRealPath("");
b. 得到地址栏地址:request.getRequestURL() ;
d. 得到相对地址:request.getRequestURI() ;
5. 附两个读取文件的方式
a. 需要打包 成jar,读取指定文件
1 packagenet.sf.jtmt.tokenizers;2
3 importjava.io.BufferedReader;4 importjava.io.IOException;5 importjava.io.InputStreamReader;6 importjava.net.URL;7
8 public classFileReader {9 public staticString read(String fileName) {10 StringBuffer sb = newStringBuffer();11 URL url = null;12 BufferedReader br = null;13 try{14 url = FileReader.class.getResource(fileName);15 br = new BufferedReader(newInputStreamReader(url.openStream(),16 "UTF-8"));17 String str;18
19 while ((str = br.readLine()) != null) {20 sb.append(str+"\n");21 }22 } catch(Exception ex) {23 ex.printStackTrace();24 } finally{25 try{26 if (br != null)27 br.close();28 } catch(IOException e) {29 //TODO Auto-generated catch block
30 e.printStackTrace();31 }32
33 }34 returnsb.toString();35 }36 }
FileReader
1 packagewisers.samza.report.util;2
3 importjava.io.File;4 importjava.io.IOException;5 importorg.apache.commons.io.FileUtils;6
7
8 public classFileReaderTest {9
10 public static voidmain(String[] args) {11 String fileStr = "";12 try{13 fileStr = FileUtils.readFileToString(newFile(14 "src/main/resources/stopwords.txt"), "UTF-8");15 } catch(IOException e) {16 e.printStackTrace();17 }18 System.out.println(fileStr);19 }20 }
View Code
b. 读取properties ,支持去读多个properties(指定默认)
1 packagewisers.samza.report.util;2
3 importjava.io.BufferedInputStream;4 importjava.io.File;5 importjava.io.FileInputStream;6 importjava.io.IOException;7 importjava.io.InputStream;8 importjava.util.HashMap;9 importjava.util.Map;10 importjava.util.Properties;11
12 /**
13 * Created by zf on 2016/8/12 0012.14 */
15 public classProConfigUtil {16
17 private static final String filePath = "config/config.properties";18 private static Map propertiesMap = new HashMap<>();19
20 privateProConfigUtil() {21
22 }23
24 public staticProConfigUtil getInstance() {25 returngetInstance(filePath);26 }27
28 private staticProConfigUtil getInstance(String filePath) {29 Properties prop =propertiesMap.get(filePath);30 if (prop == null) {31 prop = newProperties();32 try{33 String userDir = System.getProperty("user.dir");34 //WEB-INF/class35 //String userDir = ClassLoader.getSystemClassLoader().getResource("").getPath();
36 String basePath = userDir + "/src/main/";37 File file = new File(basePath +filePath);38 InputStream inputStream = newFileInputStream(file);39 InputStream in = newBufferedInputStream(inputStream);40 prop.load(in);41 } catch(IOException e) {42 e.printStackTrace();43 }44 propertiesMap.put(filePath, prop);45 }46 returnclassHolder.proConfigUtil;47 }48
49 public staticString readValue(String filePath, String key) {50 returngetInstance(filePath).propertiesMap.get(filePath).getProperty(key);51 }52
53 publicString readValue(String key) {54 returnpropertiesMap.get(filePath).getProperty(key);55 }56
57 private static classclassHolder {58
59 private static ProConfigUtil proConfigUtil = newProConfigUtil();60 }61
62
63 }
View Code