1515
1616/**
1717 * @ClassName HostUtil
18- * @Description TODO
1918 * @Author HeGuojian
2019 * @Date 2021/8/30 17:14
2120 * @Version 1.0
2221 **/
2322public class HostUtil {
24- private static final Path outPath ;
25-
26- static {
27- outPath = Paths .get (System .getProperty ("user.dir" ) ,"hosts" );
23+
24+ public static void toFile (Map <String , String > stringStringMap , Path path ) throws IOException {
25+ write (buildContent (stringStringMap ), path );
2826 }
29- private static List <String > buildContent (Map <String , String > ips ) throws IOException {
27+ private static List <String > buildContent (Map <String , String > ips ) {
3028 //C:\Windows\System32\drivers\etc
3129 Path inPath = Paths .get ("C:" , "Windows" , "System32" , "drivers" , "etc" , "hosts" );
3230 List <String > content = new ArrayList <>();
@@ -61,12 +59,17 @@ private static List<String> buildContent(Map<String, String> ips) throws IOExcep
6159 }
6260 continue ;
6361 }
64- // todo 读取原github配置,并用新的值去覆盖
62+ /*
63+ * 用ips的值覆盖hosts的值
64+ * */
65+ ips .forEach ((key , value ) -> {
66+ contentGithub .put (key , value );
67+ });
6568 List <String > contentStr = new ArrayList <>();
6669 // 添加 github 的配置
6770 contentStr .add ("#github-faster 配置 #" );
6871 // map 转 string
69- ips .forEach ((key , value ) -> {
72+ contentGithub .forEach ((key , value ) -> {
7073 StringBuffer tap = new StringBuffer ();
7174 int num = 30 - value .length ();
7275 while (num > 0 ){
@@ -82,22 +85,22 @@ private static List<String> buildContent(Map<String, String> ips) throws IOExcep
8285 return content ;
8386 }
8487
85- private static void openFolder () {
88+ private static void openFolder (Path path ) {
8689 try {
8790 String [] cmd = new String [5 ];
8891 cmd [0 ] = "cmd" ;
8992 cmd [1 ] = "/c" ;
9093 cmd [2 ] = "start" ;
9194 cmd [3 ] = " " ;
92- cmd [4 ] = outPath .toString ();
95+ cmd [4 ] = path .toString ();
9396 Runtime .getRuntime ().exec (cmd );
9497 } catch (IOException e ) {
9598 e .printStackTrace ();
9699 }
97100 }
98101
99- public static void write (List <String > content ) throws IOException {
100- File outDir = outPath .toFile ();
102+ public static void write (List <String > content , Path path ) throws IOException {
103+ File outDir = path .toFile ();
101104 if (!outDir .exists ()){
102105 outDir .mkdirs ();
103106 }
@@ -106,21 +109,18 @@ public static void write(List<String> content) throws IOException {
106109 hosts .createNewFile ();
107110 }
108111 Files .write (hosts .toPath (),content );
109- }
110-
111- private static void unlockInWin (File file ) {
112- String cmd = "C:\\ myProgram\\ fast-github\\ cmd\\ hostWritable" ;
113- try {
114- Process p = Runtime .getRuntime ().exec (cmd );
115- p .waitFor ();
116- } catch (IOException | InterruptedException e ) {
117- throw new RuntimeException (e );
118- }
112+ openFolder (path );
119113 }
120114
121- public static void toFile (Map <String , String > stringStringMap ) throws IOException {
122- List <String > content = buildContent (stringStringMap );
123- write (content );
124- openFolder ();
125- }
115+
116+
117+ /* private static void unlockInWin(File file) {
118+ String cmd = "C:\\myProgram\\fast-github\\cmd\\hostWritable";
119+ try {
120+ Process p = Runtime.getRuntime().exec(cmd);
121+ p.waitFor();
122+ } catch (IOException | InterruptedException e) {
123+ throw new RuntimeException(e);
124+ }
125+ }*/
126126}
0 commit comments