java获取src文件路径怎么写
1.java中获取src的路径怎么写
在java中获得文件的路径在我们做上传文件操作时是不可避免的。
web 上运行 1:this.getClass().getClassLoader().getResource("/").getPath(); this.getClass().getClassLoader().getResource("").getPath(); 得到的是 ClassPath的绝对URI路径。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/System.getProperty("user.dir");this.getClass().getClassLoader().getResource(".").getPath(); 得到的是 项目的绝对路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war2:this.getClass().getResource("/").getPath(); this.getClass().getResource("").getPath(); 得到的是当前类 文件的URI目录。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/this.getClass().getResource(".").getPath(); X 不 能运行3:Thread.currentThread().getContextClassLoader().getResource("/").getPath()Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的绝对URI路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/Thread.currentThread().getContextClassLoader().getResource(".").getPath() 得到的是 项目的绝对路径。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war在本地运行中1:this.getClass().getClassLoader().getResource("").getPath(); this.getClass().getClassLoader().getResource(".").getPath(); 得到的是 ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classesthis.getClass().getClassLoader().getResource(".").getPath(); X 不 能运行2:this.getClass().getResource("").getPath(); this.getClass().getResource(".").getPath(); 得到的是当前类 文件的URI目录。如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper//D:/myProjects/hp/WebRoot/WEB-INF/classes/ 得到的是 ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes3:Thread.currentThread().getContextClassLoader().getResource(".").getPath()Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classesThread.currentThread().getContextClassLoader().getResource("/").getPath() X 不 能运行最后在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。还有request.getContextPath(); 在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。
2.java项目如何获取src目录以外的目录文件啊
src是根据class编译环境的相对路径查找的,外面的路径可以使用绝对路径。
绝对路径是指文件在硬盘上真正存在的路径。例如“bg.jpg”这个图片是存放在硬盘的“E:\book\网页布局代码\第2章”目录下,那么 “bg.jpg”这个图片的绝对路径就是“E:\book\网页布\代码\第2章\bg.jpg"。
为了避免这种隋况发生,通常在网页里指定文件时,都会选择使用相对路径。所谓相对路径,就是相对于自己的目标文件位置。例如上面的例子,“s1.htm” 文件里引用了“bg.jpg”图片,由于“bg.jpg”图片相对于“s1.htm”来说,是在同一个目录的,那么要在“s1.htm”文件里使用以下代 码后,只要这两个文件的相对位置没有变(也就是说还是在同一个目录内),那么无论上传到Web服务器的哪个位置,在浏览器里都能正确地显示图片。
3.java获取某个文件夹的路径怎么写
File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:
public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\baidu");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。
下面是上面程序在我电脑上的输出:
G:\xhuoj\konw\.\src\baidu
G:\xhuoj\konw\src\baidu
4.java怎么获取src目录下所有的包名,类名,方法名 以及通过一个类名
给你代码。
这个类直接放到eclipse中可以运行,
如果不是,
File root = new File(System.getProperty("user.dir") + "\\src");
改成你指写的src目录,其编译好的类也要在classpath中,才能运行。
---------------------------------------------------------------------------------------------------
import java.io.File;
import java.lang.reflect.Method;
public class LoopApp {
public static void main(String[] args) throws Exception {
String packageName = "";
File root = new File(System.getProperty("user.dir") + "\\src");
loop(root, packageName);
}
public static void loop(File folder, String packageName) throws Exception {
File[] files = folder.listFiles();
for (int fileIndex = 0; fileIndex < files.length; fileIndex++) {
File file = files[fileIndex];
if (file.isDirectory()) {
loop(file, packageName + file.getName() + ".");
} else {
listMethodNames(file.getName(), packageName);
}
}
}
public static void listMethodNames(String filename, String packageName) {
try {
String name = filename.substring(0, filename.length() - 5);
Object obj = Class.forName(packageName + name);
Method[] methods = obj.getClass().getDeclaredMethods();
System.out.println(filename);
for (int i = 0; i < methods.length; i++) {
System.out.println("\t" + methods[i].getName());
}
} catch (Exception e) {
System.out.println("exception = " + e.getLocalizedMessage());
}
}
}
java获取java文件的路径怎么写
1. java获取某个文件夹的路径怎么写
File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:
public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\baidu");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。
下面是上面程序在我电脑上的输出:
G:\xhuoj\konw\.\src\baidu
G:\xhuoj\konw\src\baidu
2. java怎么获取文件的相对路径
一个例子,如复果有一个Test文件夹里面有test.java和hello.txt.如果你想用test.java操作制hello.txt只要在test.java中这样写2113File file=new File("hello.txt");//这样就是相对路径。
如果文件结构是Test文件夹。 |------test.java。
|------hello.txt。 |------source文件夹。
.. |---------world.txt如果想5261在test.java中操作world.txt。只要这样写File file=new File("source/world.txt");另外,在web开发中/代表项目文件夹根目录,当然也有可能代替webapps,区分方法是:如果/开头4102的uri是给浏览器解析则/代表webapps,如果是给服务1653器后台解析,则代表项目文件。
3. java 根据文件获取文件名及路径的方法
我写了一段遍历某个文件查找指定文件的,你自己改成你需要的功能。
import java.io.File;
import java.util.HashMap;
public class Test1 {
static HashMap<String, String> filelist=new HashMap<String, String>();
/**
* 递归方法
* @param path 文件路径
*/
public static void find(String path){
File file=new File(path);
File[] files = file.listFiles();
//如果文件数组为null则返回
if (files == null)
return;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
//判断是不是文件夹,如果是文件夹则继续向下查找文件
find(files[i].getAbsolutePath());
} else {
//记录文件路径
String filePath = files[i].getAbsolutePath().toLowerCase();
//记录文件名
String fileName=files[i].getName().toLowerCase();
// System.out.println("---"+strFileName);
filelist.put(fileName, filePath);
}
}
}
public static void main(String[] args) {
//需要遍历的路径,也就是你要查找文件所在的路径
String path="D:\\kpi\\";
find(path);
System.out.println("kpi.9的路径:"+filelist.get("kpi.9"));
//输出结果:d:\kpi\kpi.9
}
}
转载请注明出处育才学习网 » java获取当前文件上层文件路径
育才学习网