今天用Java连接Linux终端的时候,由于linux服务器有多台,每次运行修改代码太麻烦所以想起在resources下创建一个配置文件,然后再在主函数中加载,不难但是很容易出错,这里写一种比较简答方便操作的方法
相比较网上的我觉得这个应该是最简单也是最好理解记忆的。
新建资源文件
写入资源文件
字符串类型也不需要加引号
加载资源文件
//创建配置对象 Properties properties = new Properties(); String path = LinuxShell.class.getResource("LinuxUserIfo.properties").getPath(); //构造输入流 InputStream in = new FileInputStream(path); //加载配置文件 properties.load(in); //测试一下 String ip = properties.getProperty("linux_ip"); 运行结果