hosts 文件添加器 图形化添加ip和域名对应

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://xxlcube.blog.csdn.net/article/details/8111936
hosts <wbr>文件添加器 <wbr> <wbr> <wbr>图形化添加ip和域名对应

Hosts.java
-------------------------
package cn.edu.ustc;

public class Hosts {

private String ip;
private String url;

public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}
}
-------------------------
HostsUtil.java
-------------------------
package cn.edu.ustc;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HostsUtil {

private static Map line = new HashMap();

public static String findIP(String str) {
Pattern p = Pattern
.compile("[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}");
Matcher m = p.matcher(str);
m.reset(str);
String ip = "";
while (m.find()) {
int start = m.start();
int end = m.end();
ip = str.substring(start, end);
}
return ip;
}

public static Map readHosts() {
Properties p = new Properties();
try {
p.load(new FileReader(new File("src/cn/edu/ustc/config.properties")));
BufferedReader br = new BufferedReader(new FileReader(
p.getProperty("path")));
String str = "";
while ((str = br.readLine()) != null) {
if (str.trim().indexOf("#") != 0 && str.trim() != null
&& !"".equals(str.trim())) {
str = str.trim();
String key = findIP(str);
String value = str.substring(
str.indexOf(key) + key.length()).trim();
line.put(key, value);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}

public static boolean check(Hosts host) {
if (readHosts().containsKey(host.getIp()) || readHosts().containsValue(host.getUrl())) {
return false;
} else {
return true;
}
}

public static void writeHosts(Hosts host) {
Properties p = new Properties();
try {
p.load(new FileReader(new File("src/cn/edu/ustc/config.properties")));
RandomAccessFile raf = new RandomAccessFile(p.getProperty("path"),
"rw");
long count = raf.length();
raf.seek(count);
raf.writeBytes("\n" + host.getIp() + "\t\t" + host.getUrl());
} catch (IOException e) {
e.printStackTrace();
}
}

public static void print(Map list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.values());
}
}

}
-------------------------
MainFrame.java
-------------------------
package cn.edu.ustc;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MainFrame {

private static JFrame frame = new JFrame("Hosts文件添加器");
private static BorderLayout bl = new BorderLayout(10,10);
private static JPanel panel1 = new JPanel(new FlowLayout());//1
private static JPanel panel2 = new JPanel(new FlowLayout());//2
private static JPanel panel3 = new JPanel(new FlowLayout());//3
private static JLabel label1 = new JLabel("IP地址 :");
private static JLabel label2 = new JLabel("URL地址:");
private static JTextField text1 = new JTextField(50);
private static JTextField text2 = new JTextField(50);
private static JButton button1 = new JButton("添加");
private static JButton button2 = new JButton("清空");
private static int width = 650;

private static int height = 200;

private static int x = (int) Toolkit.getDefaultToolkit().getScreenSize()
.getWidth()
/ 2 - width / 2;

private static int y = (int) Toolkit.getDefaultToolkit().getScreenSize()
.getHeight()
/ 2 - height / 2;
private static JOptionPane pane = new JOptionPane();
private static Font font = new Font("黑体",Font.BOLD,18);
public static void main(String[] args) {
frame.setLayout(bl);
label1.setFont(font);
panel1.add(label1);
text1.setFont(font);
text1.setForeground(Color.RED);
panel1.add(text1);
label2.setFont(font);
panel2.add(label2);
text2.setFont(font);
text2.setForeground(Color.RED);
panel2.add(text2);
button1.setFont(font);
button2.setFont(font);
button1.setPreferredSize(new Dimension(150,50));
button2.setPreferredSize(new Dimension(150,50));
panel3.add(button1);
panel3.add(button2);
button1.addActionListener(new ActionListener() {
@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//添加事件
String ip = text1.getText().trim();
String url = text2.getText().trim();
Hosts host = new Hosts();
host.setIp(ip);
host.setUrl(url);
if(!"".equals(ip)&&ip!=null&&!ip.matches("[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}")){
pane.showMessageDialog(null, "请输入正确的IP地址");
}
if(null==ip||"".equals(ip)||null==url||"".equals(url)) {
pane.showMessageDialog(null, "IP和URL不能为空!");
return;
}
if(!HostsUtil.check(host)) {
pane.showMessageDialog(null, "IP:"+ip+"或URL:"+url+"在hosts文件已存在!");
return;
}else {
HostsUtil.writeHosts(host);
pane.showMessageDialog(null, "IP:"+ip+",URL:"+url+"添加成功!");
return;
}
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
text1.setText(null);
text2.setText(null);
}
});
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.CENTER);
frame.add(panel3, BorderLayout.SOUTH);
frame.setVisible(true);
frame.setLocation(x, y);
frame.setSize(width, height);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


个人链接:
--------------------------------
JavaMail实现(JAVA <wbr><wbr><wbr>SWING)新浪微博:http://weibo.com/cwtree

       JavaMail实现(JAVA <wbr><wbr><wbr>SWING)人人首页:www.renren.com/treelovexiaobei

       JavaMail实现(JAVA <wbr><wbr><wbr>SWING)优酷空间: http://i.youku.com/cwtree

       JavaMail实现(JAVA <wbr><wbr><wbr>SWING)科大首页:http://home.ustc.edu.cn/~cwtree/
--------------------------------


展开阅读全文

没有更多推荐了,返回首页