别用SwitchHosts了Java编写代码自动换github-host

这段代码是一个Java程序,用于定时从GitHub获取hosts文件并替换本地的hosts文件,以实现对GitHub的快速访问。程序包含读取本地hosts文件,下载远程hosts内容,并合并到本地文件的功能,同时提供了一个简单的批处理脚本来编译和运行Java程序。
摘要由CSDN通过智能技术生成

SwitchHosts 换github-host 需要下载exe

解决方案

使用Java编写方法

import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class HostChanger {
  public static void main(String[] args) {
    Timer timer = new Timer();
    timer.schedule(
        new TimerTask() {
          public void run() {
            HostChanger changer = new HostChanger();
            changer.changeHost();
          }
        },
        0,
        60 * 60 * 1000); // 60 minutes
  }

  public static String localhost = "C:\\Windows\\System32\\drivers\\etc\\hosts";
  public static String localhostgithub = localhost ;
  // "C:\\Windows\\System32\\drivers\\etc\\hosts-new.txt";
  //  public static String localhostgithub = "C:\\Windows\\System32\\drivers\\etc\\hosts-old.txt";
  public static String githubhost = "https://raw.hellogithub.com/hosts";

  public static void changeHost() {
    try {
      List<String> strings = readLines(localhost);
      //        github520(strings);
      strings.addAll(github520());

      //      Path src = Paths.get(localhost);
      //      Path dest = Paths.get(localhostgithub);
      //      Files.copy(src, dest, StandardCopyOption.REPLACE_EXISTING);
      //      Files.copy(src, dest, StandardCopyOption.COPY_ATTRIBUTES);
      //      Files.copy(src, dest, StandardCopyOption.ATOMIC_MOVE);

      File file = new File(localhostgithub);
      if (!file.exists()) {
        file.createNewFile();
      }
      BufferedWriter bw = new BufferedWriter(new FileWriter(localhostgithub));
      strings.forEach(
          string -> {
            try {
              bw.write(string);
              bw.newLine();
            } catch (IOException e) {
              throw new RuntimeException(e);
            }
          });
      bw.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    System.out.println("Hosts changed");
  }

  public static List<String> readLines(String fileName) throws IOException {
    List<String> lines = new ArrayList<>();
    BufferedReader reader = new BufferedReader(new FileReader(fileName));

    String line;
    while ((line = reader.readLine()) != null) {
      if (line.startsWith("# GitHub520 Host Start")) {
        // 跳过该行及下面的所有内容
        while ((line = reader.readLine()) != null) {
          if (line.startsWith("# GitHub520 主机端")) {
            // 跳过该行
            break;
          }
        }
      } else {
        // 写入该行
        lines.add(line);
      }
    }
    reader.close();
    return lines;
  }

  public static List<String> github520() throws Exception {
    List<String> lines = new ArrayList<>();
    URL url = new URL(githubhost);
    URLConnection connection = url.openConnection();
    //    connection.setRequestProperty("User-Agent", "Mozilla/5.0");
    connection.setRequestProperty("User-Agent", "curl/7.64.1");
    connection.connect();
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
      if (line.length() > 0) {
        lines.add(line);
      }
    }
    reader.close();
    return lines;
  }
}

上面代码在做一个 bat程序 美哉

@echo off
@REM  javaw HostChanger  后台运行
javac -encoding UTF-8 HostChanger.java
java HostChanger
@pause

有些类可以自己注释掉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值