⑴ java html file 获得file文件本地路径 要简单点的方法
找URL
关于绝对路径和相对路径:
绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz est.txt 代表了test.txt文件的绝对路径。http://www.sun.com/index.htm也代表了一个URL绝对路径。相对路径:相对与某个基准目录的路径。包含Web的相对路径(HTML中的相对目录),例如:在Servlet中,"/"代表Web应用的跟目录。和物理路径的相对表示。例如:"./" 代表当前目录,"../"代表上级目录。这种类似的表示,也是属于相对路径。另外关于URI,URL,URN等内容,请参考RFC相关文档标准。RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax,(http://www.ietf.org/rfc/rfc2396.txt)2.关于JSP/Servlet中的相对路径和绝对路径。2.1服务器端的地址服务器端的相对地址指的是相对于你的web应用的地址,这个地址是在服务器端解析的(不同于html和javascript中的相对地址,他们是由客户端浏览器解析的)
第一种:
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
结果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin
获取当前类的所在工程路径;
如果不加“/”
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程序里用runtime调用上级目录下的EXE文件,并且有参数命令,如何使用相对路径
那就把exe上传到项目里,然后用程序在项目中的路径,这样就不必改来改去了。当然你也可以采用配置的方法,譬如properties文件,把位置信息存在properties文件中,路径动态的读取出来,同样可以达到目的。
⑶ 通过java获取当前项目路径
getClass().getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)
例如 项目在/D:/workspace/MainStream/Test
在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/
publicStringgetCurrentPath(){
//取得根目录路径
StringrootPath=getClass().getResource("/").getFile().toString();
//当前目录路径
StringcurrentPath1=getClass().getResource(".").getFile().toString();
StringcurrentPath2=getClass().getResource("").getFile().toString();
//当前目录的上级目录路径
StringparentPath=getClass().getResource("../").getFile().toString();
returnrootPath;
}
⑷ 我运行Java是遇到的问题,怎样获得管理员权限,在win7家庭普通版下面
右键点你的编程软件,选择属性,在兼容性页面。选上已管理员身份运行此程序。然后确定。以后就可以了