Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4.  
  5.  
  6. /**
  7. * Created by user on 3/30/2017.
  8. */
  9. public class test {
  10.  
  11. private static String filePath = "test7.txt";
  12. protected HashMap<String, String> map = new HashMap<String, String>();
  13.  
  14.  
  15.  
  16. public static void main(String[] args) throws IOException{
  17. new test().clearHosts();
  18.  
  19. new test().addHost("google.com", "174.73.95.94");
  20.  
  21. new test().deleteHost("google.com");
  22.  
  23.  
  24. }
  25.  
  26. private void readHosts() throws IOException{
  27. String line;
  28. BufferedReader reader = new BufferedReader((new FileReader(filePath)));
  29. while ((line = reader.readLine()) != null) {
  30. String[] parts = line.trim().split(" ");
  31. if (!line.startsWith("#")) {
  32. String value = parts[0];
  33. String key = parts[1];
  34. map.put(key, value);
  35. }
  36. }
  37. reader.close();
  38. }
  39.  
  40. private void writeHosts(HashMap<String, String> map) throws IOException {
  41. FileWriter writer = new FileWriter("test7.txt");
  42. for(Map.Entry<String, String> entry : map.entrySet()) {
  43. writer.write(entry.getValue() + " " + entry.getKey() + "\n");
  44. }
  45. writer.close();
  46.  
  47. }
  48.  
  49. public void addHost(String host, String ip) throws IOException{
  50. readHosts();
  51. if (!map.containsKey(host)) {
  52. map.put(host, ip);
  53. }
  54. writeHosts(map);
  55. }
  56.  
  57. public void deleteHost(String host) throws IOException{
  58. readHosts();
  59. if (map.containsKey(host)) {
  60. map.remove("google.com");
  61. }
  62. writeHosts(map);
  63. }
  64.  
  65. public void createHosts(){
  66.  
  67. }
  68.  
  69. public void clearHosts() throws IOException{
  70. HashMap<String, String> map1 = new HashMap<String, String>();
  71. map1.put("localhost", "127.0.0.1");
  72. map1.put("::1", "localhost");
  73. writeHosts(map1);
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement