package com.tansun;
public class ResourceStream {
public static void main(String[] args) throws Exception {
/**
* ResourceStream.class.getResourceAsStream 是从class所在目录获取文件
* ResourceStream.class.getClassLoader().getResourceAsStream是从根路径获取文件
* 获取文件的路径,
* 1、如果 1100300000304.xml文件在当前class文件所在的目录下则可用dis3的获取方式
* 2、如果在1100300000304.xml文件加绝对路径则可用dis1的方式获取。
* 3、如果1100300000304.xml文件在当前class文件下的resource目录下则刻意dis4的方式获取
* 4、如果用类加载器获取,则可用dis2的方式获取。注意com/tansun/1100300000304.xml不能写成/com/tansun/1100300000304.xml
*/
DataInputStream dis1 = new DataInputStream(ResourceStream.class.getResourceAsStream("/com/tansun/1100300000304.xml"));
DataInputStream dis3 = new DataInputStream(ResourceStream.class.getResourceAsStream("1100300000304.xml"));
DataInputStream dis4 = new DataInputStream(ResourceStream.class.getResourceAsStream("resource/1100300000304.xml"));
DataInputStream dis2 = new DataInputStream(ResourceStream.class.getClassLoader().getResourceAsStream("com/tansun/1100300000304.xml"));
byte[] buffer = new byte[dis1.available()];
dis1.readFully(buffer);
dis1.close();
System.out.println(new String(buffer, "utf-8"));
}
}