java 读取文件的路径

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 package net.sf.jtmt.tokenizers;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.net.URL;
 7 
 8 public class FileReader {
 9     public static String read(String fileName) {
10         StringBuffer sb = new StringBuffer();
11         URL url = null;
12         BufferedReader br = null;
13         try {
14             url = FileReader.class.getResource(fileName);
15             br = new BufferedReader(new InputStreamReader(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         return sb.toString();
35     }
36 }
FileReader
 1 package wisers.samza.report.util;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import org.apache.commons.io.FileUtils;
 6 
 7 
 8 public class FileReaderTest {
 9 
10     public static void main(String[] args) {
11         String fileStr = "";
12         try {
13             fileStr = FileUtils.readFileToString(new File(
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 package wisers.samza.report.util;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 import java.util.HashMap;
 9 import java.util.Map;
10 import java.util.Properties;
11 
12 /**
13  * Created by zf on 2016/8/12 0012.
14  */
15 public class ProConfigUtil {
16 
17     private static final String filePath = "config/config.properties";
18     private static Map<String, Properties> propertiesMap = new HashMap<>();
19 
20     private ProConfigUtil() {
21 
22     }
23 
24     public static ProConfigUtil getInstance() {
25         return getInstance(filePath);
26     }
27 
28     private static ProConfigUtil getInstance(String filePath) {
29         Properties prop = propertiesMap.get(filePath);
30         if (prop == null) {
31             prop = new Properties();
32             try {
33                 String userDir = System.getProperty("user.dir");
34                 // WEB-INF/class
35                 //String userDir = ClassLoader.getSystemClassLoader().getResource("").getPath();
36                 String basePath = userDir + "/src/main/";
37                 File file = new File(basePath + filePath);
38                 InputStream inputStream = new FileInputStream(file);
39                 InputStream in = new BufferedInputStream(inputStream);
40                 prop.load(in);
41             } catch (IOException e) {
42                 e.printStackTrace();
43             }
44             propertiesMap.put(filePath, prop);
45         }
46         return classHolder.proConfigUtil;
47     }
48 
49     public static String readValue(String filePath, String key) {
50         return getInstance(filePath).propertiesMap.get(filePath).getProperty(key);
51     }
52 
53     public String readValue(String key) {
54         return propertiesMap.get(filePath).getProperty(key);
55     }
56 
57     private static class classHolder {
58 
59         private static ProConfigUtil proConfigUtil = new ProConfigUtil();
60     }
61 
62 
63 }
View Code

 

posted @ 2017-07-25 15:41  执风  阅读(439)  评论(0编辑  收藏  举报