事件背景:为了测试数据提交后,需要在另一个环境的多个测试节点下去验证测试数据是否添加成功,找了一大堆放法,用了比较笨的方法实现了。不多废话思路如下:

为了万无一失,先备份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文件的更多相关文章

  1. windows下的host文件在哪里?做什么用的?

    在Window系统中有个Hosts文件(没有后缀名),在Windows98系统下该文件在Windows目录,在Windows2000/XP系统中位于C:\Winnt\System32\Drivers\ ...

  2. 如何修改windows系统的host文件

    方法/步骤   首先我们打开我的计算机     然后我们进入C盘的C:\Windows\System32\drivers\etc这个目录下面找到host这个文件   双点击打开,选择计算本打开,这时可 ...

  3. 怎么样修改win7下的host文件

    由于在访问tensorflow官网时访问不了,需要修改hosts文件,然而win7下因为权限问题导致不能修改hosts文件,解决方法如下: 1.先复制hosts文件到别的地方,修改完后再覆盖回来.中间 ...

  4. windows下的host文件在哪里,有什么作用?

    在Window系统中有个Hosts文件(没有后缀名),在Windows98系统下该文件在Windows目录,在Windows2000/XP系统中位于C:\Winnt\System32\Drivers\ ...

  5. Windows 下目录及文件向Linux同步

    本文解决的是Windows 下目录及文件向Linux同步的问题,Windows向 Windows同步的请参考:http://www.idcfree.com/article-852-1.html 环境介 ...

  6. Windows下如何将一个文件夹通过Git上传到GitHub上(转)

    在通过windows系统的电脑上写代码,需要将项目上传到GitHub上去.比如在Pycharm上写Django后端,整个项目是一个文件夹的形式,那么怎么才能这个文件夹通过Git命令上传到GitHub上 ...

  7. 用脚本如何实现将Linux下的txt文件批量转化为Windows下的txt文件?

    众所周知,Windows和Linux的文件换行回车格式不同,Windows下换行格式是\r\n(回车+换行),Linux下换行格式为\n(只是换行),因此,其中一个操作系统的文本文件若需要在另外一个中 ...

  8. windows下python检查文件是否被其它文件打开

    windows下python检查文件是否被其它文件打开.md 有时候我们需要能够判断一个文件是否正在被其它文件访问,几乎不可避免的要调用操作系统接口 from ctypes import cdll i ...

  9. Windows下创建.gitgnore文件

    相信使用过git的朋友可能遇到过,直接在windows下创建.gitgnore文件失败.类似截图那样 上网查了一下,有两种方法. 方法1: 此方法较为简单,前提是安装了git bash. 用git b ...

随机推荐

  1. File类操作文件

    简单示例: public static void main(String[] args) { // 列出系统所有的根路径 File[] listRoots = File.listRoots(); fo ...

  2. jQuery笔记(二)

    $()下的常用方法 addClass():添加样式 removeClass():删除样式 $('div').addClass('box2 box4'); $('div').removeClass('b ...

  3. Alpha冲刺第4天

    Alpha第四天 1.团队成员 郑西坤 031602542 (队长) 陈俊杰 031602504 陈顺兴 031602505 张胜男 031602540 廖钰萍 031602323 雷光游 03160 ...

  4. C语言入门:03.关键字、标识符、注释

    一.学习语法之前的提醒 (1)C语言属于一门高级语言,其实,所有高级语言的基本语法组成部分都是一样的,只是表现形式不太一样 (2)就好像亚洲人和非洲人,大家都有人类的结构:2只 手.2只脚.1个头,只 ...

  5. WebAPI框架里设置异常返回格式统一

    直接上代码 /// <summary> /// 消息代理处理,用来捕获这些特殊的异常信息 /// </summary> public class CustomErrorMess ...

  6. 初识elasticsearch_1(基本概念和基本操作)

    初识 ElasticSearch是一个基于Lucene的搜索服务器,它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.本博客部分基于es的官方文档.es的官方文档网址如下:h ...

  7. java的类属性默认有this 但容易与参数重名 所以需要显性的加上this 以分区别

  8. 【luogu3768】简单的数学题 欧拉函数(欧拉反演)+杜教筛

    题目描述 给出 $n$ 和 $p$ ,求 $(\sum\limits_{i=1}^n\sum\limits_{j=1}^nij\gcd(i,j))\mod p$ . $n\le 10^{10}$ . ...

  9. 【设计模式】—— 创建者模式Builder

    前言:[模式总览]——————————by xingoo 模式意图 一个对象的创建十分复杂,为了区分构建过程和使用过程,因此分开.使用一个Director类进行对象的创建,Builder规定了这个创建 ...

  10. myeclipse运行错误

    错误出现为: !MESSAGE Product com.genuitec.myeclipse.product.ide could not be found. 这是在我将其它电脑上的myeclipse拷 ...