45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:IP地址管理系统源代码大全

IP地址管理系统源代码大全

2016-08-25 15:44:23 来源:www.45fan.com 【

IP地址管理系统源代码大全

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.StringTokenizer;

/*
* Created on 2005-1-12
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @authorStarshus
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class IPBook
{
static boolean hasIP;
public static void main(String [] args)
throws Exception
{
int choice = -1;
while (choice != 0)
{
IPBook ipBook = new IPBook();
System.out.print("1.增加IP地址/n" +
"2.查找IP地址/n" +
"3.删除IP地址/n" +
"0.退出程序/n输入:");
BufferedReader in =
new BufferedReader (
new InputStreamReader (System.in));
String string = in.readLine();
choice = Integer.valueOf(string).intValue();
switch(choice)
{
case 1:{
System.out.print("请以IP标准格式输入IP:/n" +
"比如127.0.0.1/n" +
"输入:");
BufferedReader input =
new BufferedReader (
new InputStreamReader (System.in));
String str = input.readLine();
System.out.println(str);
ipBook.writeIP(str);
}break;
case 2:{
System.out.print("请输入你要查找IP地址的头地址/n" +
"比如你要查找127.0.0.1的话就请输入127/n" +
"输入:");
BufferedReader input =
new BufferedReader (
new InputStreamReader (System.in));
String str = input.readLine();
long l = Integer.valueOf(str).longValue();
ipBook.findIP(l);
if(hasIP)
{
String result = ipBook.findIP(l);
System.out.println(result);
}else{
System.out.println("没有找到:(");
}
}break;
case 3:{
System.out.print("请输入你要删除IP地址的头地址/n" +
"比如你要删除127.0.0.1的话就请输入127/n" +
"输入:");
BufferedReader input =
new BufferedReader (
new InputStreamReader (System.in));
String str = input.readLine();
long l = Integer.valueOf(str).longValue();
ipBook.deleteIP(l);break;
}
}
}
}
public void inputIP(String ip)
throws Exception
{
try{
BufferedReader in1 =
new BufferedReader (
new FileReader("IPBook.txt"));
String s1 = "",s2 = "";
while ((s1 =in1.readLine())!= null)
{
s1 = s1+"/n";
s2 = s2 + s1;
}
s2 = s2 + ip;
DataInputStream in2 =
new DataInputStream(
new ByteArrayInputStream((s2.getBytes())));
DataOutputStream out =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("IPBook.txt")));
while (in2.available() != 0)
{
out.write(in2.read());
}
in2.close();
out.close();
System.out.println("写"+ip+"到文件IPBook.txt中!");
}catch(Exception e){}
}
public String findIP(long ip)
throws Exception
{
hasIP=true;
String readLine ="";
String result="";
try{
BufferedReader in =
new BufferedReader (
new FileReader("IPBook.txt"));
long top;
while ( (readLine = in.readLine())!=null)
{
top = topTokenizer(readLine);
if (top == ip)
{
result += readLine+"/n";
}
}
if(result=="")
hasIP= false;
}catch(Exception e){}
return result;
}
public long topTokenizer(String ip)
throws Exception
{
String top = "";
try{
StringTokenizer st = new StringTokenizer (ip,".");
top = st.nextToken();
}catch(Exception e){}
return Integer.valueOf(top).longValue();
}
public boolean isIP(String str)
throws Exception//判断格式是否正确
{
try{
StringTokenizer st = new StringTokenizer (str ,".");
String s1="",s2="",s3="",s4="";
s1 = st.nextToken();
s2 = st.nextToken();
s3 = st.nextToken();
s4 = st.nextToken();
}
catch(Exception e){}
return true;
}
public void writeIP(String ip)//向文件中加写IP地址
throws Exception
{
try{
if(isIP(ip))
{
try{
inputIP(ip);
}catch(IOException e){}
}else
System.err.println("请正确输入IP地址!");
}catch(Exception e){}
}
public void deleteIP(long ip)//用头地址删除ip
throws Exception
{
String find = "";
String ss = "";
findIP(ip);
if(hasIP)
{
try{
find = findIP(ip);
String temp = "";
BufferedReader in =
new BufferedReader(
new FileReader("IPBook.txt"));
while ( (temp=in.readLine())!=null )
{
if(topTokenizer(temp)!=ip)
{
ss += temp+"/n";
}
}
BufferedReader in2 =
new BufferedReader(
new StringReader(ss));
String sss = "";
PrintWriter out =
new PrintWriter (
new BufferedWriter (
new FileWriter("IPBook.txt")));
while ( (sss = in2.readLine()) != null)
{
out.println(sss);
}
out.close();
System.out.println("已删除.");
}
catch(Exception e){}
}else{
System.out.println("没有找到:(");
}
}
}


本文地址:http://www.45fan.com/a/question/67489.html
Tags: 一个 简单 地址
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部