
正文
java源代码一级目录名 mainclassjava源代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
在java类中怎么获得java项目的目录
一 相对路径的获得
说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目)
String relativelyPath=System.getProperty("user.dir");
上述相对路径中,java项目中的文件是相对于项目的根目录
web项目中的文件路径视不同的web服务器不同而不同(tomcat是相对于 tomcat安装目录\bin)
二 类加载目录的获得(即当运行时某一类时获得其装载目录)
1.1)通用的方法一(不论是一般的java项目还是web项目,先定位到能看到包路径的第一级目录)
InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt");
(test.txt文件的路径为 项目名\src\test.txt;类TestAction所在包的第一级目录位于src目录下)
上式中将TestAction,test.txt替换成对应成相应的类名和文件名字即可
1.2)通用方法二 (此方法和1.1中的方法类似,不同的是此方法必须以'/'开头,参考)
InputStream is=Test1.class.getResourceAsStream("/test.txt");
(test.txt文件的路径为 项目名\src\test.txt,类Test1所在包的第一级目录位于src目录下)
三 web项目根目录的获得(发布之后)
1 从servlet出发
可建立一个servlet在其的init方法中写入如下语句
ServletContext s1=this.getServletContext();
String temp=s1.getRealPath("/"); (关键)
结果形如:D:\工具\Tomcat-6.0\webapps\002_ext\ (002_ext为项目名字)
如果是调用了s1.getRealPath("")则输出D:\工具\Tomcat-6.0\webapps\002_ext(少了一个"\")
2 从httpServletRequest出发
String cp11111=request.getSession().getServletContext().getRealPath("/");
结果形如:D:\工具\Tomcat-6.0\webapps\002_ext\
四 classpath的获取(在Eclipse中为获得src或者classes目录的路径)
方法一 Thread.currentThread().getContextClassLoader().getResource("").getPath()
eg: String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println("t---"+t);
输出:t---/E:/order/002_ext/WebRoot/WEB-INF/classes/
方法二 JdomParse.class.getClassLoader().getResource("").getPath() (JdomParse为src某一个包中的类,下同)
eg:String p1=JdomParse.class.getClassLoader().getResource("").getPath();
System.out.println("JdomParse.class.getClassLoader().getResource--"+p1);
输出: JdomParse.class.getClassLoader().getResource--/E:/order/002_ext/WebRoot/WEB-INF/classes/
另外,如果想把文件放在某一包中,则可以 通过以下方式获得到文件(先定位到该包的最后一级目录)
eg String p2=JdomParse.class.getResource("").getPath();
System.out.println("JdomParse.class.getResource---"+p2);
输出: JdomParse.class.getResource---/E:/order/002_ext/WebRoot/WEB-INF/classes/jdom/ (JdomParse为src目录下jdom包中的类)
四 属性文件的读取:
方法 一
InputStream in = lnew BufferedInputStream( new FileInputStream(name)); Properties p = new Properties(); p.load(in);
注意路径的问题,做执行之后就可以调用p.getProperty("name")得到对应属性的值
方法二
Locale locale = Locale.getDefault();
ResourceBundle localResource = ResourceBundle.getBundle("test/propertiesTest", locale);
String value = localResource.getString("test");
System.out.println("ResourceBundle: " + value);
工程src目录下propertiesTest.properties(名字后缀必须为properties)文件内容如下:
test=hello word
相关问答
Q1: Java工程下各个目录都是什么意思,里面放些什么东西?web,JS resource,Java resource等等。
java resourcejava源代码一级目录名:Java源代码、配置文件、资源文件
WebContent/Web-inf/lib放应用所需java源代码一级目录名的库文件
WebContent/目录下可以放html、jsp、ftl、css、js、图片、文本等(看具体要求)
Q2: 用java编写程序,键盘输入一个目录名称,要求分别输出该目录中所有子目录名和以exam开头的文件名。
public static void findFileList(File dir,HashSet dirs,HashSet fileNames ) {
if (!dir.exists() || !dir.isDirectory()) {// 判断是否存在目录
return;
}
String[] files = dir.list();// 读取目录下的所有目录文件信息
for (int i = 0; i files.length; i++) {// 循环,添加文件名或回调自身
File file = new File(dir, files[i]);
if (file.isFile() file.getName().startsWith("exam")) {// 如果文件
fileNames.add(dir + "\\" + file.getName());
} else {// 如果是目录
dirs.add(dir);
findFileList(file,dirs,fileNames);// 回调自身继续查询
}
}
}
public static void main(String[] args) {
HashSet dirs = new HashSet();
HashSet fileNames = new HashSet();
findFileList(new File("D:\\zhidao"),dirs,fileNames);
System.out.println("目录:"+dirs);
System.out.println("exam开头的文件:"+fileNames);
}
Q3: java源程序编译过后的文件是什么文件
java源程序文件名是*.java(源代码就在*.java里)
编译后为*.class(class是二进制文件)
java虚拟机(JVM)运行程序的时候就是加载class文件,因此说java程序都是由class堆起来的
Q4: java中源代码在哪个文件夹中
什么样的源代码啊???纯java的源代码的话就是都可以运行
直接打** .java就可以了
如果是一个案例那就不一样了,那都是类,比如说一个项目,通过就是jsp页面去调用已设计好的类,那样有的就执行不了,只能编译
有什么问题可以通过百度hi找我,随时在线
Q5: java源代码文件使用什么目录作为一级目录
File[]
childs
=
f.listFiles();
//listFiles()返回目录下java源代码一级目录名的所有文件这句下面加上if(childs!=null){
for(){
}}如果传入的那个目录是不存在的话java源代码一级目录名,childs就是null的,所以会报错
关于java源代码一级目录名和mainclassjava源代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







