你好,提问者:
指定资源路径的方法有两种:
一种是绝对路径专,一种是相对路径。
获取当前类的所属在工程路径;
Filef=newFile(this.getClass().getResource("/").getPath());
System.out.println(f);
获取当前类的绝对路径;
Filef=newFile(this.getClass().getResource("").getPath());
System.out.println(f);
获取当前类的所在工程路径;
Filedirectory=newFile("");//参数为空
StringcourseFile=directory.getCanonicalPath();
System.out.println(courseFile);
获取当前工程src目录下selected.txt文件的路径:
URLxmlpath=this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);
『贰』 java怎么获取本地文件路径
Java中获取用户本地路径的方法:
用request对象来获取:request.getRequestURL();
或者用:request.getRequestURI();
『叁』 Java中如何获取系统路径
File.separatorChar 返回一个字符,表示当前系统默认的文件名分隔符,在Windows中为"/",unix中为"/"。 File.separator 与前者相同,但将分隔符作为字符串类型返回。 pathSeparatorChar 返回一个字符,表示当前系统默认的路径名分隔符,在Windows中为";",unix中为":"。 File.pathSeparator 与前者相同,但将分隔符作为字符串类型返回。 import java.lang.*; import java.io.*; public class test { public static void main(String[] args) { System.out.println(File.separatorChar); System.out.println(File.separator);
『肆』 java获取某个文件夹的路径怎么写
File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:
public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\");
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\
G:\xhuoj\konw\src\
『伍』 JAVA中如何得到文件路径
java文件中获得路径
Thread.currentThread().getContextClassLoader().getResource("") //获得资源文件(.class文件)所在路径
ClassLoader.getSystemResource("")
Class_Name.class.getClassLoader().getResource("")
Class_Name.class .getResource("/")
Class_Name.class .getResource("") // 获得当前类所在路径
System.getProperty("user.dir") // 获得项目根目录的绝对路径
System.getProperty("java.class.path") //得到类路径和包路径
打印输出依次如下:
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/com/xml/imp/
F:\work_litao\uri_test
F:\work_litao\uri_test\WebContent\WEB-INF\classes;F:\work_litao\uri_test\WebContent\WEB-INF\lib\dom4j.jar
『陆』 通过java获取当前项目路径
getClass().getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)
例如 项目在/D:/workspace/MainStream/Test
在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/
publicStringgetCurrentPath(){
//取得根目录路径
StringrootPath=getClass().getResource("/").getFile().toString();
//当前目录路径
StringcurrentPath1=getClass().getResource(".").getFile().toString();
StringcurrentPath2=getClass().getResource("").getFile().toString();
//当前目录的上级目录路径
StringparentPath=getClass().getResource("../").getFile().toString();
returnrootPath;
}
『柒』 java如何获取用户本地路径
importjava.io.File;
importjavax.swing.filechooser.FileSystemView;
publicclassTest{
publicstaticvoidmain(String[]args){
FileSystemViewfsv=FileSystemView.getFileSystemView();
//将桌面的那个文件目录赋值给file
Filefile=fsv.getHomeDirectory();
//输出桌面那个目录的路径
System.out.println(file.getPath());
}
}
『捌』 java路径获得几种方法.txt
Java获取路径的各种方法:
(1)、request.getRealPath("/");//不推荐使用获取工程的根路径
(2)、request.getRealPath(request.getRequestURI());//获取jsp的路径,这个方法比较好用,可以直接在servlet和jsp中使用
(3)、request.getSession().getServletContext().getRealPath("/");//获取工程的根路径,这个方法比较好用,可以直接在servlet和jsp中使用
(4)、 this.getClass().getClassLoader().getResource("").getPath();//获取工程classes 下的路径,这个方法可以在任意jsp,servlet,java文件中使用,因为不管是jsp,servlet其实都是java程序,都是一个 class。所以它应该是一个通用的方法。