java怎样获取当前目录路径

java怎样获取当前目录路径,第1张

很多朋友都想知道java如何获取当前目录路径?下面就一起来了解一下吧~

1、利用System.getProperty()函数获取当前路径:

System.out.println(System.getProperty("user.dir"))//user.dir指定了当前的路径

2、使用File提供的函数获取当前路径:

File directory = new File("")//设定为当前文件夹 try{ System.out.println(directory.getCanonicalPath())//获取标准的路径 System.out.println(directory.getAbsolutePath())//获取绝对路径 }catch(Exceptin e){} File.getCanonicalPath()和File.getAbsolutePath()大约只是对于new File(".")和new File("..")两种路径有所区别。 # 对于getCanonicalPath()函数,“."就表示当前的戚物尺文件夹,而”..“则表示当前文件夹的上一级文件夹 # 对于getAbsolutePath()函数,则不管”.”、“..”,返回当前的路径加上你在new File()时设定的路径 # 至于getPath()函数,得到的只是你在new File()时设定的路径高高 比如当前的路径为 C:/test : File directory = new File("abc") directory.getCanonicalPath() //得蚂友到的是C:/test/abc directory.getAbsolutePath() //得到的是C:/test/abc direcotry.getPath() //得到的是abc File directory = new File(".") directory.getCanonicalPath() //得到的是C:/test directory.getAbsolutePath() //得到的是C:/test/. direcotry.getPath() //得到的是. File directory = new File("..") directory.getCanonicalPath() //得到的是C:/ directory.getAbsolutePath() //得到的是C:/test/.. direcotry.getPath() //得到的是.. 另外:System.getProperty()中的字符串参数如下: System.getProperty()参数大全 # java.version Java Runtime Environment version # java.vendor Java Runtime Environment vendor # java.vendor.url Java vendor URL # java.home Java installation directory # java.vm.specification.version Java Virtual Machine specification version # java.vm.specification.vendor Java Virtual Machine specification vendor # java.vm.specification.name Java Virtual Machine specification name # java.vm.version Java Virtual Machine implementation version # java.vm.vendor Java Virtual Machine implementation vendor # java.vm.name Java Virtual Machine implementation name # java.specification.version Java Runtime Environment specification version # java.specification.vendor Java Runtime Environment specification vendor # java.specification.name Java Runtime Environment specification name # java.class.version Java class format version number # java.class.path Java class path # java.library.path List of paths to search when loading libraries # java.io.tmpdir Default temp file path # java.compiler Name of JIT compiler to use # java.ext.dirs Path of extension directory or directories # os.name Operating system name # os.arch Operating system architecture # os.version Operating system version # file.separator File separator ("/" on UNIX) # path.separator Path separator (":" on UNIX) # line.separator Line separator ("/n" on UNIX) # user.name User’s account name # user.home User’s home directory # user.dir User’s current working directory

JAVA中获取路径 关键字: java中获取路径

1、jsp中取得路径:

以工程名为TEST为例:

(1)得到包含工程名的当前页面全路径:request.getRequestURI() 结果:/TEST/test.jsp (2)得到工程名:request.getContextPath() 结果:/TEST (3)得到当前页面所在目录下全名称:request.getServletPath() 结果:如果页面在jsp目录下 /TEST/jsp/test.jsp (4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp") 结果:D:/resin/webapps/TEST/test.jsp (5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent() 结果:D:/resin/webapps/TEST

2、在类中取得路径: (1)类的绝对路径:Class.class.getClass().getResource("/").getPath() 结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/ (2)得到工程的路径:System.getProperty("user.dir") 结果:D:/TEST

如何获取指定目录包含的文件和子目录 DirectoryInfo GetFiles() 获取目录中(不包含子目录)的文件 返回类型为FileInfo[] 支持通配符查找 DirectoryInfo GetDirectories() 获取目录(不包含子目录)的子目录 返回类型为DirectoryInfo[] 支持通配符查找 DirectoryInfo GetFileSystemInfos() 获取指定目录下(不包含子目录)的文件和子目录 返回类型为FileSystemInfo[] 支持通配符查找 如何获取指定文件的基本信息 FileInfo Exists 获取晌肢指定文件是否存在 FileInfo Name FileInfo Extensioin 获取文件的名称和扩展名 FileInfo FullName 获取文件的全限定名称(完整路径) FileInfo Directory 获取文件所在目录 返回类型为DirectoryInfo FileInfo DirectoryName 获取文件所在目录的路径(完整路径) FileInfo Length 获取文件的大小(字节数) FileInfo IsReadOnly 获取文件是否只读 FileInfo Attributes 获取或设置指定文件的属性 返回类型为FileAttributes枚举 可以是多个值的组合 FileInfo CreationTime FileInfo LastAccessTime FileInfo LastWriteTime 分别用于获取文件的创建时间 访问时间 修改时间 遍历文件夹 文件夹是树形结构 遍历算法宴模世有 广度优先级和深度优先级 区别 广度首先查找同一层目录 深度首先遍历一条分支 有了这个区别 就可以确定链表的插入位置 即广度遍历插入点总在末尾 深度遍历插入点在首部 详细看代码 public static void searchFile(String path List<Object>resultList) { File file = new File(path)if (file isDirectory()) { LinkedList<File[]>levelLinked = new LinkedList<码携File[]>()levelLinked add(file listFiles())do { File[] childFiles = levelLinked remove( )for (File cf : childFiles) { if (cf isDirectory()) { // 此处控制遍历的方向 levelLinked add(cf listFiles())// 广度 // levelLinked add( cf listFiles())//深度 } else { String fileName = cf getName()// 文件名 有后缀 String filePath = cf getAbsolutePath()// 绝对路径 String fileParent = cf getParent()// 上层路径 注意最后的 / // 比较算法 找到后放入集合 String[] sf = { fileName fileParent filePath }resultList add(sf)} } } while (levelLinked size() >)} } 此处加入一些API提供的工具类 希望有用 多后缀判断 如 java class js等 String[] hz = {java class js}Arrays sort(hz)//下面的搜索需要排序 例如 class的顺序在java之前 如果不sort() class文件不会被查找 Arrays binarySearch(hz fn_)//fn_文件后缀 如果明确目录的层次结构简单 递归算法也是不错的选择 个人觉得 文件的搜索 应该单独起线程

//////////////////////// 采用递归的方式遍历 文件夹和子文件中的所有文件 public void FindFile(string dirPath) //参数dirPath为指定的目录 { //在指定目录及子目录下查找文件 在listBox 中列出子目录及文件 DirectoryInfo Dir=new DirectoryInfo(dirPath)try { foreach(DirectoryInfo d in Dir GetDirectories()//查找子目录 { FindFile(Dir+d ToString()+"")listBox Items Add(Dir+d ToString()+"")//listBox 中填加目录名 } foreach(FileInfo f in Dir GetFiles("* ")) //查找文件 { listBox Items Add(Dir+f ToString())//listBox 中填加文件名 } } catch(Exception e) { MessageBox Show(e Message)}

}

用下面代码限制文件的类型 foreach(FileInfo f in Dir GetFiles("* ")) //查找文件

“* ”指要访问的文件的类型的扩展名 /////////////////////////////// 代码改成如下 会不会好一点

DirectoryInfo TheFolder=new DirectoryInfo(folderFullName)

DirectoryInfo[] dirInfo = TheFolder GetDirectories()//遍历文件夹 foreach(DirectoryInfo NextFolder in dirInfo) this listBox Items Add(NextFolder Name)

lishixinzhi/Article/program/net/201311/14311

既然文件名不知道是什么,用directory.getfiles去取得该目录下的文件,就可以得到瞎升咐源文件名了.

System.IO.Directory

GetFiles(String)

返回指定目录中文件的名称(包括其路径)。

例子:

string

targetDirectory

=

"c:\\windows"

//例如

string

[]

fileEntries

=

Directory.GetFiles(targetDirectory)

foreach(string

fileName

in

fileEntries)

这里就可以得到该文件名称磨简老了.


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

原文地址: https://outofmemory.cn/tougao/8171428.html

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

发表评论

登录后才能评论

评论列表(0条)

保存