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

在java项目中如何获取某个文件的路径,第1张

File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如filegetAbsolutePath()其中file是File的实例对象。下面是一个具体例子:

public class PathTest

{

    public static void main(String[] args)

    {

        File file = new File("\\src\\baidu");

        Systemoutprintln(filegetAbsolutePath());

        try

        {

            Systemoutprintln(filegetCanonicalPath());

        } catch (IOException e)

        {

            eprintStackTrace();

        }

    }

}

getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。

下面是上面程序在我电脑上的输出:

G:\xhuoj\konw\\src\baidu

G:\xhuoj\konw\src\baidu

在java中获得文件的路径在我们做上传文件 *** 作时是不可避免的。web上运行1:thisgetClass()getClassLoader()getResource("/")getPath();thisgetClass()getClassLoader()getResource("")getPath();得到的是ClassPath的绝对URI路径。如:/D:/jboss-422GA/server/default/deploy/hpwar/WEB-INF/classes/SystemgetProperty("userdir");thisgetClass()getClassLoader()getResource("")getPath();得到的是项目的绝对路径。如:/D:/jboss-422GA/server/default/deploy/hpwar2:thisgetClass()getResource("/")getPath();thisgetClass()getResource("")getPath();得到的是当前类文件的URI目录。如:/D:/jboss-422GA/server/default/deploy/hpwar/WEB-INF/classes/com/jebel/helper/thisgetClass()getResource("")getPath();X不能运行3:ThreadcurrentThread()getContextClassLoader()getResource("/")getPath()ThreadcurrentThread()getContextClassLoader()getResource("")getPath()得到的是ClassPath的绝对URI路径。如:/D:/jboss-422GA/server/default/deploy/hpwar/WEB-INF/classes/ThreadcurrentThread()getContextClassLoader()getResource("")getPath()得到的是项目的绝对路径。如:/D:/jboss-422GA/server/default/deploy/hpwar在本地运行中1:thisgetClass()getClassLoader()getResource("")getPath();thisgetClass()getClassLoader()getResource("")getPath();得到的是ClassPath的绝对URI路径。如:/D:/myProjects/hp/WebRoot/WEB-INF/classesthisgetClass()getClassLoader()getResource("")getPath();X不能运行2:thisgetClass()getResource("")getPath();thisgetClass()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:ThreadcurrentThread()getContextClassLoader()getResource("")getPath()ThreadcurrentThread()getContextClassLoader()getResource("")getPath()得到的是ClassPath的绝对URI路径。。如:/D:/myProjects/hp/WebRoot/WEB-INF/classesThreadcurrentThread()getContextClassLoader()getResource("/")getPath()X不能运行最后在Web应用程序中,我们一般通过ServletContextgetRealPath("/")方法得到Web应用程序的根目录的绝对路径。还有requestgetContextPath();在Weblogic中要用requestgetServletContext()getContextPath();但如果打包成war部署到Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。

当使用 Java API *** 作 HDFS 时,可以使用 FileSystemlistFiles() 方法来获取文件列表。该方法接受一个 Path 对象,表示要列举文件的目录,并返回一个 RemoteIterator<LocatedFileStatus> 对象,该对象可用于迭代目录中的文件。

例如,下面的代码演示了如何使用 listFiles() 方法来获取 HDFS 上的文件列表:

// 定义 HDFS 连接配置

Configuration conf = new Configuration();

// 获取 HDFS FileSystem 对象

FileSystem fs = FileSystemget(conf);

// 定义要列举文件的目录

Path dirPath = new Path("/user/hadoop");

// 获取文件列表

RemoteIterator<LocatedFileStatus> fileIter = fslistFiles(dirPath, true);

// 遍历文件列表

while (fileIterhasNext()) {

// 获取当前文件信息

LocatedFileStatus fileStatus = fileIternext();

// 输出文件名称和大小

Systemoutprintln(fileStatusgetPath()getName() + " : " + fileStatusgetLen());

}

(1)、requestgetRealPath("/");//不推荐使用获取工程的根路径

(2)、requestgetRealPath(requestgetRequestURI());//获取jsp的路径,这个方法比较好用,可以直接在servlet和jsp中使用

(3)、requestgetSession()getServletContext()getRealPath("/");//获取工程的根路径,这个方法比较好用,可以直接在servlet和jsp中使用

(4)、 thisgetClass()getClassLoader()getResource("")getPath();//获取工程classes 下的路径,这个方法可以在任意jsp,servlet,java文件中使用,因为不管是jsp,servlet其实都是java程序,都是一个 class。所以它应该是一个通用的方法。

0、关于绝对路径和相对路径

1基本概念的理解绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例 如:C:xyz esttxt 代表了testtxt文件的绝对路径。>

第一种:

File f = new File(thisgetClass()getResource("/")getPath());

Systemoutprintln(f);

结果:

C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin

获取当前类的所在工程路径;

如果不加“/”

File f = new File(thisgetClass()getResource("")getPath());

Systemoutprintln(f);

结果:

C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test

获取当前类的绝对路径;

第二种:

File directory = new File("");//参数为空

String courseFile = directorygetCanonicalPath() ;

Systemoutprintln(courseFile);

结果:

C:\Documents and Settings\Administrator\workspace\projectName

获取当前类的所在工程路径;

第三种:

URL xmlpath = thisgetClass()getClassLoader()getResource("selectedtxt");

Systemoutprintln(xmlpath);

结果:

file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selectedtxt

获取当前工程src目录下selectedtxt文件的路径

第四种:

Systemoutprintln(SystemgetProperty("userdir"));

结果:

C:\Documents and Settings\Administrator\workspace\projectName

获取当前工程路径

第五种:

Systemoutprintln( SystemgetProperty("javaclasspath"));

结果:

C:\Documents and Settings\Administrator\workspace\projectName\bin

获取当前工程路径

以上就是关于在java项目中如何获取某个文件的路径全部的内容,包括:在java项目中如何获取某个文件的路径、java 项目如何获取项目所在的物理根路径、使用Java API *** 作HDFS时,_方法用于获取文件列表等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10059763.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存