package main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class start {
public static void main(String[] args) throws Exception {
/*File 中可以获取到的路径 jar 中获取不到文件路径*/
//1、start.class.getResource("../").getPath()+"conf/xconf.xml"
//2、start.class.getClassLoader().getResource("conf/xconf.xml").getPath()
//3、File filePath = new File(""); String courseFile = filePath.getCanonicalPath() ;courseFile+"/src/conf/xconf.xml"
//4、Thread.currentThread().getContextClassLoader().getResource("conf/xconf.xml").getPath()
//5、new File("./src/conf/xconf.xml").getAbsolutePath()
//6、"./src/conf/xconf.xml"
Resource res=new Resource();
String xmlStr = res.getResource();
System.out.println(xmlStr);
Map<String, String> map = new HashMap<String, String>();
StringReader sr = new StringReader(xmlStr);
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
NodeList nodeList = doc.getElementsByTagName("stor");
for (int i=0;i<nodeList.getLength();i++) {
Node node = nodeList.item(i);
String content = node.getTextContent();
String [] data = content.split("\\|");
if(data.length==0){
return;
}else{
map.put(new String(Base64.decode(data[0])), new String(Base64.decode(data[1])));
}
}
System.out.println(map);
}
}
}
Java 获取jar包中工程下的文件路径的问题
最新推荐文章于 2024-08-28 15:51:22 发布