如何动态修改windows下的host文件
事件背景:为了测试数据提交后,需要在另一个环境的多个测试节点下去验证测试数据是否添加成功,找了一大堆放法,用了比较笨的方法实现了。不多废话思路如下:
为了万无一失,先备份hosts文件内容:
1.读取hosts所有文本内容,代码如下
/**
* 获取文件全部内容
* @param fileName
* @return
*/
public String readToString(String fileName) {
String encoding = "UTF-8";
File file = new File(fileName);
Long filelength = file.length();
byte[] filecontent = new byte[filelength.intValue()];
try {
FileInputStream in = new FileInputStream(file);
in.read(filecontent);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
return new String(filecontent, encoding);
} catch (UnsupportedEncodingException e) {
logger.error("The OS does not support " + encoding);
e.printStackTrace();
return null;
}
}
2.清空hosts文件内容:
/**
* 清空文本内容
* @param fileName
*/
public void clearInfoForFile(String fileName) {
File file =new File(fileName);
try {
if(!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter =new FileWriter(file);
fileWriter.write("");
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
3.追加一行hosts,也可以多个,视情况写入
/**
* 在已有的文件后面追加信息
* @param fileName
* @param info
*/
public void appendInfoToFile(String fileName, String info) {
File file =new File(fileName);
try {
if(!file.exists()){
file.createNewFile();
}
FileWriter fileWriter =new FileWriter(file, true);
fileWriter.write(info);
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
4.打开hosts绑定网站,这里我们以百度为例,提示无法连接。
5.在清空hosts文件,将备份的原内容写入即可完成还原hosts文件操作
6.测试代码如下:
@Test
public void run(){
String hosts =readToString("C:/Windows/System32/drivers/etc/hosts");
clearInfoForFile("C:/Windows/System32/drivers/etc/hosts");
appendInfoToFile("C:/Windows/System32/drivers/etc/hosts", "127.0.0.1 www.baidu.com");
System.setProperty("webdriver.chrome.driver", "tools/chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.baidu.com/");
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
clearInfoForFile("C:/Windows/System32/drivers/etc/hosts");
appendInfoToFile("C:/Windows/System32/drivers/etc/hosts", hosts);
}
笨方法实现,先Mark下。
如何动态修改windows下的host文件的更多相关文章
- windows下的host文件在哪里?做什么用的?
在Window系统中有个Hosts文件(没有后缀名),在Windows98系统下该文件在Windows目录,在Windows2000/XP系统中位于C:\Winnt\System32\Drivers\ ...
- 如何修改windows系统的host文件
方法/步骤 首先我们打开我的计算机 然后我们进入C盘的C:\Windows\System32\drivers\etc这个目录下面找到host这个文件 双点击打开,选择计算本打开,这时可 ...
- 怎么样修改win7下的host文件
由于在访问tensorflow官网时访问不了,需要修改hosts文件,然而win7下因为权限问题导致不能修改hosts文件,解决方法如下: 1.先复制hosts文件到别的地方,修改完后再覆盖回来.中间 ...
- windows下的host文件在哪里,有什么作用?
在Window系统中有个Hosts文件(没有后缀名),在Windows98系统下该文件在Windows目录,在Windows2000/XP系统中位于C:\Winnt\System32\Drivers\ ...
- Windows 下目录及文件向Linux同步
本文解决的是Windows 下目录及文件向Linux同步的问题,Windows向 Windows同步的请参考:http://www.idcfree.com/article-852-1.html 环境介 ...
- Windows下如何将一个文件夹通过Git上传到GitHub上(转)
在通过windows系统的电脑上写代码,需要将项目上传到GitHub上去.比如在Pycharm上写Django后端,整个项目是一个文件夹的形式,那么怎么才能这个文件夹通过Git命令上传到GitHub上 ...
- 用脚本如何实现将Linux下的txt文件批量转化为Windows下的txt文件?
众所周知,Windows和Linux的文件换行回车格式不同,Windows下换行格式是\r\n(回车+换行),Linux下换行格式为\n(只是换行),因此,其中一个操作系统的文本文件若需要在另外一个中 ...
- windows下python检查文件是否被其它文件打开
windows下python检查文件是否被其它文件打开.md 有时候我们需要能够判断一个文件是否正在被其它文件访问,几乎不可避免的要调用操作系统接口 from ctypes import cdll i ...
- Windows下创建.gitgnore文件
相信使用过git的朋友可能遇到过,直接在windows下创建.gitgnore文件失败.类似截图那样 上网查了一下,有两种方法. 方法1: 此方法较为简单,前提是安装了git bash. 用git b ...
随机推荐
- 组件 -- Alert
alert的背景色: alert-primary alert-secondary alert-success . . . .alert : 警告框类 .data-dismiss = "ale ...
- HDU 2088 Box of Bricks
http://acm.hdu.edu.cn/showproblem.php?pid=2088 Problem Description Little Bob likes playing with his ...
- 基于 Redis 做分布式锁
基于 REDIS 的 SETNX().EXPIRE() 方法做分布式锁 setnx() setnx 的含义就是 SET if Not Exists,其主要有两个参数 setnx(key, value) ...
- docker cp 和docker exec 查看docker 运行的容器信息
1. 使用docker 运行 redis 和 postgresql docker run -d -p : -v /redis/data/:/data redis docker run -d -p : ...
- 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...
- 实战基于Spring Boot 2的WebFlux和mLab搭建反应式Web
Spring Framework 5带来了新的Reactive Stack非阻塞式Web框架:Spring WebFlux.作为与Spring MVC并行使用的Web框架,Spring WebFlux ...
- Java时间的转换
String DATE_FORMAT = "yyyy-MM-dd"; LocalDate localDate = LocalDate.now(); long startTime = ...
- CUDA ---- 线程配置
前言 线程的组织形式对程序的性能影响是至关重要的,本篇博文主要以下面一种情况来介绍线程组织形式: 2D grid 2D block 线程索引 矩阵在memory中是row-major线性存储的: 在k ...
- [代码]--SQLServer数据库的一些全局变量
select APP_NAME ( ) as w --当前会话的应用程序 select @@IDENTITY --返回最后插入的标识值 select USER_NAME() --返回用户数据 ...
- Problem A: Dreamweaver 解题报告
人生首道非传统题给了交互... 考试的时候花了半小时搞清楚了这东西是啥,然后打了10分的暴力...但并没有拿到分. 而且一直不会本地测试... 捣鼓捣鼓了好久,问了人才知道.. 我本地没装g++,下发 ...