java读取src下的文件(properties,xml,file)

  1. properties
        
            /**   
    
             * 读取配置文件  
    
             * @return
    
             */
    
    private Properties loadProperty() {
    
    Properties prop=new Properties();
    
    try {
    
    	//FileInputStream is=new FileInputStream("config.properties");
    
    	InputStream is=this.getClass().getResourceAsStream("/config.properties");
    
    	prop.load(is);
    
    	is.close();
    
    } catch (IOException e) {
    
    	e.printStackTrace();
    
    }
    
    return prop;
    
    }
    
    2.读取xml
    生成一个文件对象:  
    
    File file = new File(getClass().getClassLoader().getResource("test.xml").getPath());  
      
    直接得到一个输入流:  
    InputStream in = getClass().getClassLoader().getResourceAsStream("test.xml");
    
      
    
    3.读取其他
  

String path=new Test().getClass().getClassLoader().getResource("a").getPath() ;

System.out.println(path);

File f=new File("src/templates");

System.out.println(f.getAbsolutePath());
0%