zoukankan      html  css  js  c++  java
  • 如何动态修改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下。

  • 相关阅读:
    10分钟学会Python
    Python接口自动化(二)接口开发
    Python接口自动化(一)接口基础
    去掉webstorm内容区域右侧的一条竖线
    webstorm识别element-ui的标签
    vue中点击复制粘贴功能 clipboard 移动端
    vue pc element-ui class
    禁止浏览器记住密码
    js 将网络图片格式转为base64 canvas 跨域
    移动端网页在本地服务器调试
  • 原文地址:https://www.cnblogs.com/longronglang/p/7606383.html
Copyright © 2011-2022 走看看