
FileseparatorChar 返回一个字符,表示当前系统默认的文件名分隔符,在Windows中为"/",unix中为"/"。Fileseparator 与前者相同,但将分隔符作为字符串类型返回。pathSeparatorChar 返回一个字符,表示当前系统默认的路径名分隔符,在Windows中为";",unix中为":"。FilepathSeparator 与前者相同,但将分隔符作为字符串类型返回。import javalang;import javaio;public class test {public static void main(String[] args) {Systemoutprintln(FileseparatorChar);Systemoutprintln(Fileseparator);
第一种:
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
获取当前工程路径
我觉得如果只是为了得到路径,那491064739的回答其实已经给你思路了,就是传入个File f,然后String s = fgetAbsolutePath();得到绝对路径!不过,似乎你问的是Path后,我的思路是传入两个参数,一个workspace的路径,一个是File f 。伪代码如下:
public String getPathInfo(String workspace,File file) throw Exception{
String path = filegeAbsolute(); //绝对路径
pathreplaceAll("\\\\","/"); //把\ 替换成 /
workspacereplaceAll("\\\\","/");
String info = pathsubString(workspacelength-1); //-1是留下/
//因为是绝对路径,所以文件名最前面的就是workspace,把前面那段去掉就是Path后的了
return info;
}
大致上逻辑就是这样吧
import javaioFile;
import javaxswingfilechooserFileSystemView;
public class Test {
public static void main(String[] args) {
FileSystemView fsv=FileSystemViewgetFileSystemView();
//将桌面的那个文件目录赋值给file
File file=fsvgetHomeDirectory();
//输出桌面那个目录的路径
Systemoutprintln(filegetPath());
}
}
写了一个读取本地文件的方法, File file = new File(htmlFile); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while((s=brreadLine())!=null){ aladd(s); } 在当前类写了main方法测试了一下是可行的, 但是页面某方法想调用该方法,不能实现。 总结问题是:只有放在static方法中可行,在其他地方调用都显示找不到指定文件。 文件结构: 把本地文件放在了web-inf的classes下了,相对路径写的(“/filetxt”); 求教为啥static方法可以,其他地方调用不行,这个函数本身不是静态的啊。
以上就是关于Java中如何获取系统路径全部的内容,包括:Java中如何获取系统路径、java中获取工程中res目录路径的方法、java 获取当前文件的路径,路径全名等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)