今天用Java连接Linux终端的时候,由于linux服务器有多台,每次运行修改代码太麻烦所以想起在resources下创建一个配置文件,然后再在主函数中加载,不难但是很容易出错,这里写一种比较简答方便操作的方法
相比较网上的我觉得这个应该是最简单也是最好理解记忆的。
新建资源文件
![](https://img-blog.csdnimg.cn/img_convert/40b4a39c703f9acf288c3761e38ef80b.png)
写入资源文件
字符串类型也不需要加引号
![](https://img-blog.csdnimg.cn/img_convert/9e6b1414a0242a5f2e4d911b8ca051b0.png)
加载资源文件
//创建配置对象
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");
运行结果
![](https://img-blog.csdnimg.cn/img_convert/9dbc3e85e889c27987340a4ba8be53a3.png)