之前有过方法 传参时 参数是固定的, 现在因为有改动,所以做了一个properties文件来写参数,具体实现非常简单,
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
public class UtilFTP {
//读取.properties 结尾的配置文件用,getP, getParam
public static Map<String,String> getP(String path) throws Exception{
Resource resource = new ClassPathResource(path);
Properties props = PropertiesLoaderUtils.loadProperties(resource);
Map<String,String> param = new HashMap<String,String>((Map) props);
return param;
}
}
我们需要这样一个工具类。
实现的话调用工具类就可以了。
import java.util.Map;
public class test {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Map<String, String> app =UtilFTP.getP("FTP.properties");
String url=app.get("url");
int port =Integer.valueOf(app.get("port"));
String username =app.get("username");
String password =app.get("password");
String remotePath =app.get("remotePath");
String localPath =app.get("localPath");
System.out.println(url+port+username+password+remotePath+localPath);
}
我们在用的时候只需要将对应的properties 文件名字传入就可以了。