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,读取指定文件
package net.sf.jtmt.tokenizers; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL; public class FileReader {
public static String read(String fileName) {
StringBuffer sb = new StringBuffer();
URL url = null;
BufferedReader br = null;
try {
url = FileReader.class.getResource(fileName);
br = new BufferedReader(new InputStreamReader(url.openStream(),
"UTF-8"));
String str; while ((str = br.readLine()) != null) {
sb.append(str+"\n");
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
return sb.toString();
}
}
FileReader
package wisers.samza.report.util; import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils; public class FileReaderTest { public static void main(String[] args) {
String fileStr = "";
try {
fileStr = FileUtils.readFileToString(new File(
"src/main/resources/stopwords.txt"), "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(fileStr);
}
}
b. 读取properties ,支持去读多个properties(指定默认)
package wisers.samza.report.util; import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; /**
* Created by zf on 2016/8/12 0012.
*/
public class ProConfigUtil { private static final String filePath = "config/config.properties";
private static Map<String, Properties> propertiesMap = new HashMap<>(); private ProConfigUtil() { } public static ProConfigUtil getInstance() {
return getInstance(filePath);
} private static ProConfigUtil getInstance(String filePath) {
Properties prop = propertiesMap.get(filePath);
if (prop == null) {
prop = new Properties();
try {
String userDir = System.getProperty("user.dir");
// WEB-INF/class
//String userDir = ClassLoader.getSystemClassLoader().getResource("").getPath();
String basePath = userDir + "/src/main/";
File file = new File(basePath + filePath);
InputStream inputStream = new FileInputStream(file);
InputStream in = new BufferedInputStream(inputStream);
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
propertiesMap.put(filePath, prop);
}
return classHolder.proConfigUtil;
} public static String readValue(String filePath, String key) {
return getInstance(filePath).propertiesMap.get(filePath).getProperty(key);
} public String readValue(String key) {
return propertiesMap.get(filePath).getProperty(key);
} private static class classHolder { private static ProConfigUtil proConfigUtil = new ProConfigUtil();
} }