java路径问题-getClass().getResourceAsStream()

src(源文件夹)

┣━11.properties

┗━myspider(myspider包)

┣━22.properties
┗━Test.java

 

Java代码  收藏代码
  1. package myspider;
  2. import java.io.UnsupportedEncodingException;
  3. /**
  4.  *
  5.  * @author mark
  6.  */
  7. public class Test {
  8.     public static void main(String[] args) throws UnsupportedEncodingException{
  9.         Test t=new Test();
  10.         //文件名前不加“/”,则表示从当前类所在的包下查找该资源。如下则表示的是从包myspider下查找22.properties文件资源。
  11.         System.out.println(“1:”+t.getClass().getResourceAsStream(“22.properties”));//输出java.io.BufferedInputStream@61de33
  12.         //文件名前加了“/”,则表示从类路径下也就是从classes文件夹下查找资源,如下表示从classes文件夹下查找22.properties文件资源。
  13.         System.out.println(“2:”+t.getClass().getResourceAsStream(“/22.properties”));//输出null
  14.         //文件名前加了“/”,则表示从类路径下也就是从classes文件夹下查找资源,如下表示从classes文件夹下查找11.properties文件资源。
  15.         System.out.println(“3:”+t.getClass().getResourceAsStream(“/11.properties”));//输出java.io.BufferedInputStream@14318bb
  16.         System.out.println();
  17.         //当前包路径4:file:/E:/myobject/myspider/build/classes/myspider/
  18.         System.out.println(“4:”+t.getClass().getResource(“”));
  19.         //输出当前类路径5:file:/E:/myobject/myspider/build/classes/
  20.         System.out.println(“5:”+t.getClass().getResource(“/”));
  21.         /*
  22.          * 如果类路径下的当前包有22.properties文件,则输出6:file:/E:/myobject/myspider/build/classes/myspider/22.properties
  23.          * 否者输出源文件下的22.properties文件的路径,则输出:6:file:/E:/myobject/myspider/src/myspider/22.properties
  24.          */
  25.         System.out.println(“6:”+t.getClass().getResource(“22.properties”));
  26.         /*
  27.          * 如果类路径下有11.properties文件,则输出7:file:/E:/myobject/myspider/build/classes/11.properties
  28.          * 否者输出源文件下的11.properties文件的路径,则输出:6:7:file:/E:/myobject/myspider/src/11.properties
  29.          */
  30.         System.out.println(“7:”+t.getClass().getResource(“/11.properties”));
  31.     }
  32. }

SystemConfig实现之从properties文件读取数据

根据从网上找的资料,使用properties类实现了读取配置文件信息。

import java.io.File;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
*/
public class SystemConfig {

private SystemConfig(){

}

//获取环境变量init.conf.env
private static final String ENV=System.getProperty(“init.conf.env”);
//构建配置文件名
private static final String MQ_ARTICLE_PROPERTY_FILE = “mq-article-“+ENV+”.properties”;

private static Properties props = null;

/**
* 初始化ResourceBundle对象
* @throws IOException
*/
private static void init() throws IOException{
if( null == props){
props = new Properties();
loadFile();
}
}

/**
* 加载指定名称的properties文件,约定路径为/conf/
* @param propName
* @throws IOException
*/
private static void loadFile() throws IOException{
//获取配置文件列表/conf/*.properties
File dir = new File(SystemConfig.class.getResource(“/”).getPath()+”conf/”);
String[] fileNames = dir.list();

//过滤出符合当前环境的文件
//List<String> fileList = null;
if (fileNames != null && fileNames.length > 0) {
//fileList = Lists.newArrayList();
// 包括文件,文件夹的判断
for (String fName : fileNames) {
if (fName.indexOf(ENV) != -1) {
//fileList.add(fName);
//load进props
InputStream in = SystemConfig.class.getResourceAsStream(“/conf/” + fName);
props.load(in);
}
}
}
}

/**
* 获取系统配置
* @param key
* @param defaultValue
* @return
*/
public static String getProperties(String key,String defaultValue){
try {
init();
} catch (IOException e) {
props = null;
}
if(null == props)
return defaultValue;
return props.getProperty(key) == null ? defaultValue : props.getProperty(key);
}

}

除了使用properties类的方法外,Java还提供了resourcebundle的方式实现这一功能。

但是resourcebundle的命名规则有约束,主要为了实现国际化。这里还是先不用这个了。知道能实现即可。

数据聚合类网站数据来源

数据聚合类网站数据来源

1.有些是开放api(微博) 加层壳
2.有些是公开数据(例如股票数据) 自己转数据库/api

3.有些是服务商未授权的非即时服务(例如停车位 电影信息) 机器去爬数据 转数据库/api
4.有些是服务商未授权的非即时服务(例如违章车辆) 人肉找数据转数据库/api
5.有些是服务商未授权的即时服务(例如快递) 自己转web request成api

6.有些是服务商授权提供中介的即时服务/非即时服务 (例如图灵 360 小冰机器人)

apigee对大部分小用户免费
国内这几家都贵 性价比很低
haoservice.com/apilist/
apistore.baidu.com/
w
ww.juhe.com

国外:
https://apigee.com/providers

有必要的话搞爬虫,调用api,根据自己需求定。