Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Created by user on 3/30/2017.
- */
- public class test {
- private static String filePath = "test7.txt";
- protected HashMap<String, String> map = new HashMap<String, String>();
- public static void main(String[] args) throws IOException{
- new test().clearHosts();
- new test().addHost("google.com", "174.73.95.94");
- new test().deleteHost("google.com");
- }
- private void readHosts() throws IOException{
- String line;
- BufferedReader reader = new BufferedReader((new FileReader(filePath)));
- while ((line = reader.readLine()) != null) {
- String[] parts = line.trim().split(" ");
- if (!line.startsWith("#")) {
- String value = parts[0];
- String key = parts[1];
- map.put(key, value);
- }
- }
- reader.close();
- }
- private void writeHosts(HashMap<String, String> map) throws IOException {
- FileWriter writer = new FileWriter("test7.txt");
- for(Map.Entry<String, String> entry : map.entrySet()) {
- writer.write(entry.getValue() + " " + entry.getKey() + "\n");
- }
- writer.close();
- }
- public void addHost(String host, String ip) throws IOException{
- readHosts();
- if (!map.containsKey(host)) {
- map.put(host, ip);
- }
- writeHosts(map);
- }
- public void deleteHost(String host) throws IOException{
- readHosts();
- if (map.containsKey(host)) {
- map.remove("google.com");
- }
- writeHosts(map);
- }
- public void createHosts(){
- }
- public void clearHosts() throws IOException{
- HashMap<String, String> map1 = new HashMap<String, String>();
- map1.put("localhost", "127.0.0.1");
- map1.put("::1", "localhost");
- writeHosts(map1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement