
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
把文本文件处理为数组,每行为一个数组元素,然后在每个元素中查找关键词,vbs可以直接使用instr函数来查找,也可以使用正则表达式查找。找到后把那个数组元素复制出来就可以了。第一种,使用instrc = split(createobject("scriptingfilesystemobject")opentextfile("atxt")readall,vbcrlf)for i = 0 to ubound(c)if instr(c(i),"nice") then msgbox c(i)next第二种,使用正则表达式c = split(createobject("scriptingfilesystemobject")opentextfile("atxt")readall,vbcrlf)for i = 0 to ubound(c)if rt("nice",c(i)) then msgbox c(i)nextFunction rt(patrn,str)set regex=new regexpregexpattern = patrnregexignorecase = falsert = regextest(str)End Function
以上就是关于获取文件夹下的一个文件路径怎么写全部的内容,包括:获取文件夹下的一个文件路径怎么写、如何用vbs获取指定路径下的文件名并输出到文本文件、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)