文件路径java|在java项目中如何获取某个文件的路径

文件路径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\\"); 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读取文件的路径

File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:123456789101112131415public 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文件的路径

public class Test { public static void main(String[] args) { String path = "Test.java"; File file = new File(path); System.out.println(file.getAbsoluteFile()); }} —–运行结果:D:\workspaces\studyStruts2\Test.java不加任何路径,就是指当版前路径望采纳权

④ java中怎么写文件路径

你是用myeclipse软件写JAVA的吗,你可以在myeclipse的打开窗口中依次创建项目,包,类什么的编写程序,之后你打开JAVA工作间,找到你的程序,那么地址栏里就是你的JAVA文件路径了,又或者你用dreamever软件,先制作好你的网页,连接好超链接什么的之后,打开代码,就可以看见了

⑤ java里文件路径怎么写

Filefile=newFile("D:\\123.txt");你这种不用绝对路径是不行的,只有一个方法,在web工程启动内servlet中获取到webroot路径,在servlet的init中使用StringwebRoot=getServletContext().getRealPath("/");获取,然后使用这容webRoot变量追加路径,再newFile(),这样的话要求就是,你的服务必须要启动,否则不会init,无法得到工程发布目录的相对路径

⑥ java中获取文件路径的几种方式

获取当前类的所在工程路径;如果不加“/”File f = new File(this.getClass().getResource("").getPath());System.out.println(f);结果:C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test获取当前类的绝对路径;第二种:File directory = new File("");//参数为空String courseFile = directory.getCanonicalPath() ;System.out.println(courseFile);结果:C:\Documents and Settings\Administrator\workspace\projectName获取当前类的所在工程路径;第三种:URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");System.out.println(xmlpath);结果:file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt获取当前工程src目录下selected.txt文件的路径第四种:System.out.println(System.getProperty("user.dir"));结果:C:\Documents and Settings\Administrator\workspace\projectName获取当前工程路径第五种:System.out.println( System.getProperty("java.class.path"));结果:C:\Documents and Settings\Administrator\workspace\projectName\bin获取当前工程路径

⑦ 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_testF:\work_litao\uri_test\WebContent\WEB-INF\classes;F:\work_litao\uri_test\WebContent\WEB-INF\lib\dom4j.jar

⑧ 在java项目中如何获取某个文件的路径

如果是在tomcat等服务器中运行的话,用ServletContext中的getRealPath()方法可以获取指定文件的绝对路径,如:getRealPath("/WEB-INF/db.xml");

⑨ Java 文件路径的不同写法

必须有差别看你的是什么系统win系统的分隔符是\,因为需要转义,所以为 \\

未经允许不得转载:山九号 » 文件路径java|在java项目中如何获取某个文件的路径

赞 (0)