java 判定1个IP地址是否是合法IP

测试使用工具:Eclipse

工具使用截图:

编写思路:

  1. 编写函数判定IP地址是否合法
  2. 编写验证测试方法,读入测试IP地址,调用IP判定方法,输出测试结果到文本中

IP地址判定函数

package ipJudge;

public class IPJudge {
	public static boolean isRightIP (String ip){
			//排除空值
			if(ip == null){  	
				return false;
			}
			//基于IP地址长度进行排除
			if(ip.length()<7 || ip.length()>15){     
				return false;
			}
			//避免Integer.parseInt转换时报错,大家也可以注销如下的if代码调试。
			if(ip.charAt(0) == '.'  || ip.charAt(ip.length()-1) == '.' ){
				return false;
			}
			String[] Inip= ip.split("\\.");
			if (Inip.length != 4){
				return false;
				}
			//判断所有的字符是否均是数字
			for(int i=0;i<4;i++){
				for(int j=0;j<Inip[i].length();j++){
					if(Inip[i].charAt(j) < '0' || Inip[i].charAt(j) > '9'){
						return false;
					}
				}
				}
			//IP范围进行判断(0.0.0.0-255.255.255.255)
			for(int i=0;i < Inip.length;i++){
				int temp = Integer.parseInt(Inip[i]);
				if(temp < 0 || temp > 255){
					return false;
				}
			}	
			return true;
	}
}

测试代码

package file;
import ipJudge.IPJudge;

import java.util.ArrayList;
import java.util.List;
import java.io.*;

public class FileRW {
	public static void main(String[] args){
		File file = new File("E:\\JAVA_storage\\IP测试\\IP.txt");
		File fnew = new File("E:\\JAVA_storage\\IP测试\\IP验证结果.txt");
		//文本中IP地址读入
		FileWriter fwrite=null;
		BufferedReader reader = null;
		List<String> list = new ArrayList<>();
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString =null;
			while ((tempString = reader.readLine()) != null){
				list.add(tempString);
			}
		reader.close();
		//IP地址测试结果输出到文本中
		fwrite = new FileWriter(fnew);
		StringBuffer write = new StringBuffer();
		for(String tmpIP : list){
			//调用IPjudge中IP的判定方法函数
			boolean right = IPJudge.isRightIP(tmpIP);
			System.out.println(right +  ", ip: " + tmpIP);
			write.append(right +  ", ip: " + tmpIP + "\n");
		}
		fwrite.write(write.toString());
		fwrite.flush();
		}catch (IOException e){
			e.printStackTrace();
		}finally{
			if(fwrite != null){
				try {
					fwrite.close();
				} catch (IOException e) {
					System.out.println(e);
				}
			}
		}
		
		
	}

}

IP地址测试文本

0.0.0.0
255.255.255.255
12.25.35.69
255.213.23.12
1 1.22.2.23
12323.1232.2323.12323
s.23.32.12
12.e.2.3
1.3.*.4
12.243.23.!!?
e/.RT.45.2
a.d.@.4
!?.d.feewfe.se
12.we2.12
12.3.2
12.34
12
wer2e
.32.12.23
12.23.4.
\\.dsf.22.dsf2
wwwwwwwwwwwwwwwwwwwwwwwwwwwwww.22222222222222222222222.2222222222222222222.33333333333333

IP地址测试结果文本查看

true, ip: 0.0.0.0
true, ip: 255.255.255.255
true, ip: 12.25.35.69
true, ip: 255.213.23.12
false, ip: 1 1.22.2.23
false, ip: 12323.1232.2323.12323
false, ip: s.23.32.12
false, ip: 12.e.2.3
false, ip: 1.3.*.4
false, ip: 12.243.23.!!?
false, ip: e/.RT.45.2
false, ip: a.d.@.4
false, ip: !?.d.feewfe.se
false, ip: 12.we2.12
false, ip: 12.3.2
false, ip: 12.34
false, ip: 12
false, ip: wer2e
false, ip: .32.12.23
false, ip: 12.23.4.
false, ip: \\.dsf.22.dsf2
false, ip: wwwwwwwwwwwwwwwwwwwwwwwwwwwwww.22222222222222222222222.2222222222222222222.33333333333333

 

展开阅读全文