- 浏览: 80730 次
文章分类
- 全部博客 (89)
- web service (9)
- subversion (1)
- JBOSS (3)
- interview (23)
- jQery (2)
- ExtJs (0)
- Axis (0)
- Design pattern (3)
- Agile (2)
- mutithread (0)
- Core Java (24)
- programming methods (1)
- SSH (7)
- jee design (1)
- OO (4)
- books (8)
- other (1)
- JSF (7)
- seam (2)
- Weblogic (4)
- JPA (1)
- ADF (1)
- Spring (5)
- Tomcat (1)
- DWR (2)
- JEE (3)
- Servlet (1)
- EJB (1)
- JDBC (3)
最新评论
-
iloveflower:
呵呵。好好学习。。。。。。。。。。。。
java 读书 -
Eric.Yan:
看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
java 读书
This is the simplest wat to get the File object to which a certain URL object is pointing at:
File file=new File(url.toURI());
Now, for your concrete questions:
•finding all resources in the META-INF "directory":
You can indeed get the File object pointing to this URL
Enumeration<URL> en=getClass().getClassLoader().getResources("META-INF");
if (en.hasMoreElements()) {
URL metaInf=en.nextElement();
File fileMetaInf=new File(metaInf.toURI());
File[] files=fileMetaInf.listFiles();
//or
String[] filenames=fileMetaInf.list();
}
•all resources named bla.xml (recursivly)
In this case, you'll have to do some custom code. Here is a dummy example:
final List<File> foundFiles=new ArrayList<File>();
FileFilter customFilter=new FileFilter() {
@Override
public boolean accept(File pathname) {
if(pathname.isDirectory()) {
pathname.listFiles(this);
}
if(pathname.getName().endsWith("bla.xml")) {
foundFiles.add(pathname);
return true;
}
return false;
}
};
//rootFolder here represents a File Object pointing the root forlder of your search
rootFolder.listFiles(customFilter);
When the code is run, you'll get all the found ocurrences at the foundFiles List.
********************************************
example:
private static void init() {
URL url = NTGLoggerFactory.class.getClassLoader().getResource(FILE_NAME);
PropertyConfigurator.configure(url);
initialized = true;
}
File file=new File(url.toURI());
Now, for your concrete questions:
•finding all resources in the META-INF "directory":
You can indeed get the File object pointing to this URL
Enumeration<URL> en=getClass().getClassLoader().getResources("META-INF");
if (en.hasMoreElements()) {
URL metaInf=en.nextElement();
File fileMetaInf=new File(metaInf.toURI());
File[] files=fileMetaInf.listFiles();
//or
String[] filenames=fileMetaInf.list();
}
•all resources named bla.xml (recursivly)
In this case, you'll have to do some custom code. Here is a dummy example:
final List<File> foundFiles=new ArrayList<File>();
FileFilter customFilter=new FileFilter() {
@Override
public boolean accept(File pathname) {
if(pathname.isDirectory()) {
pathname.listFiles(this);
}
if(pathname.getName().endsWith("bla.xml")) {
foundFiles.add(pathname);
return true;
}
return false;
}
};
//rootFolder here represents a File Object pointing the root forlder of your search
rootFolder.listFiles(customFilter);
When the code is run, you'll get all the found ocurrences at the foundFiles List.
********************************************
example:
private static void init() {
URL url = NTGLoggerFactory.class.getClassLoader().getResource(FILE_NAME);
PropertyConfigurator.configure(url);
initialized = true;
}
发表评论
-
Java Collection summary
2012-06-16 02:40 577Collection:List、Set Map: ... -
When to use Comparable vs Comparator
2012-06-15 00:52 792I have a list of objects I need ... -
Arrays.fill with multidimensional array in Java
2012-06-15 00:09 696How can I fill a multidimension ... -
Immutable objects
2012-06-14 23:49 720Immutable objects are simply ... -
Implementing hashCode; Transaction.java
2012-06-14 23:43 826Below is the syntax highlight ... -
Lazy initialization
2012-06-14 22:48 802http://www.javapractices.com/to ... -
How to sort an array,mid of linkedlist, reverse int
2012-06-13 07:47 939A common mistake for a beginner ... -
Java各类型转换
2012-06-13 05:25 705各种数字类型转换成字符串型: String s = Str ... -
regular expression
2012-06-13 03:08 5201、Java对反斜线处理的 ... -
string functions
2012-06-13 00:09 853import java.util.*; public c ... -
String array to arraylist
2012-06-13 00:07 587There are some important thing ... -
core java interview summary
2012-06-12 04:11 386http://blog.sina.com.cn/s/blog_ ... -
programming with String
2012-06-12 01:43 555Question: 1) Write code to che ... -
Java高手必会的要点
2012-05-29 03:28 612http://developer.51cto.com/art/ ... -
How to override equals method in Java
2012-05-12 02:57 1543Object class holds some very in ... -
Top 30 Programming interview questions
2012-05-12 02:48 911Programming questions are integ ... -
10 example of using ArrayList in Java >>> Java ArrayList Tutorial
2012-05-12 02:37 882ArrayList in Java is most frequ ... -
How to use Comparator and Comparable in Java? With example
2012-05-12 02:21 770Read more: http://javarevisited ... -
Difference between HashMap and HashTable? Can we make hashmap synchronized?
2012-05-12 01:32 778This question oftenly asked in ... -
How HashMap works in Java
2012-05-11 23:40 749Read more: http://javarevisited ...
相关推荐
因此,使用`getClass().getResourceAsStream()`或`getClass().getClassLoader().getResourceAsStream()`时,可以正确地找到这些资源。 需要注意的是,`getResourceAsStream()`方法返回的是`InputStream`,这意味着...
爬取武汉理工大学课表信息,并且显示出来,仅供参考学习
1. 使用`Class.getResource`或`ClassLoader.getResource`,这两个方法都需要传入相对类路径的字符串。 2. 使用Guava的`Resources.getResource`,它提供了一种更简洁的接口,同时支持绝对和相对路径。 理解并掌握...
Java 反射机制中getClass()和.class的使用方法示例 本文主要介绍了 Java 反射机制中getClass()和.class的使用方法,并结合实例形式分析了 Java 类反射机制的相关操作技巧。 一、获取类的反射 在 Java 中,可以...
Java ClassLoader学习总结 Java 类加载机制是 Java 中一个非常重要的机制,它负责加载 Class 文件到 JVM,以供程序使用。ClassLoader 是 Java 中的一个抽象类,它的主要作用是加载 Class 文件到 JVM 中。...
在Java类中,读取配置文件信息可以使用多种方法,其中一种方法是使用`this.getClass().getClassLoader().getResourceAsStream`方法。 `this.getClass().getClassLoader().getResourceAsStream`方法是Java类中的一种...
InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml"); try { addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile)); } catch...
import java.util.Scanner; import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.imageio.*; ...public class Jsq{ ... blaf9.setIcon(new ImageIcon(getClass().getResource(s009...
System.out.println(Test.class.getClassLoader().getResource("")); ``` 4. **使用`ClassLoader.getSystemResource("")`**: - 获取系统类加载器的资源路径。 - 示例代码: ```java System.out.println...
L_img2 = new javax.swing.JLabel(new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/tray/images/netbean2.png"))); pop = new PopupMenu(); open = new MenuItem("打开"); open....
Document document = sb.build(this.getClass().getClassLoader().getResourceAsStream(fileName)); Element root = document.getRootElement(); List list = XPath.selectNodes(root, "/beans/bean"); for...
URL resource = getClass().getResource("/path/to/resource.txt"); InputStream in = resource.openStream(); // 读取流中的数据 ``` 2. **使用`Resources`类**:`java.nio.file.Resources`类提供了一些静态...
在尝试使用Spring的`ResourceUtils.getFile()`方法读取文件时,如以下代码所示: ```java import org.springframework.util.ResourceUtils; public static void main(String[] args) throws IOException { File ...
Activiti modeler 设计器汉化文件。引入方式:修改StencilsetRestResource中的InputStream stencilsetStream = this.getClass().getClassLoader().getResourceAsStream("stencilset.json.zh-cn");
Document doc = sb.build(this.getClass().getClassLoader() .getResourceAsStream("beans.xml")); Element root = doc.getRootElement();// 获取根元素 List<?> list = root.getChildren("bean");// 获取...
- 使用当前类作为起点:`this.getClass().getResourceAsStream(path)` 或 `this.getClass().getResource(path)` - 直接使用类加载器:`ClassLoader.getSystemClassLoader().getResourceAsStream(path)` #### 三、...
(1)、request.getRealPath(“/”);//不推荐使用获取工程的根路径 (2)、...//获取工程的根路径,这个方法比较好用,可以直接在servlet和jsp中使用 (4)、 this.getClass().getClassLoader().getResource(“”).getPath();