本demo全是模拟现实的案例
所需的数据源文件链接: https://pan.baidu.com/s/1dd4AeNv4SiEToE81P6PPfA 提取码: 4qqi
import java.io.{BufferedReader, FileInputStream, InputStreamReader}
import scala.collection.mutable.ArrayBuffer
object IpUtil {
/**
* 此方法针对ip进行作业
* @param ip
* @return
*/
def ip2Long(ip: String): Long = {
val fragments = ip.split("[.]")//(192,168,12,224)
var ipNum = 0L
for (i <- 0 until fragments.length){
//将数组的每个元素转换成十进制
ipNum = fragments(i).toLong | ipNum << 8L
}
ipNum
}
/**
* 读取给定路径下的文件
* @param path
* @return
*/
def readData(path: String) = {
val br = new BufferedReader(new InputStreamReader(new FileInputStream(path)))
var s: String = null
var flag = true
val lines = new ArrayBuffer[String]()
while (flag)
{
s = br.readLine()
if (s != null)
lines += s
else
flag = false
}
lines
}
/**
* 二分查找法:数据的要求必须是已经排序好的。
* @param lines
* @param ip
* @return
* 返回值就是ip对应的区间段的索引值
*/
def binarySearch(lines: ArrayBuffer[String], ip: Long) : Int = {
var low = 0
var high = lines.length - 1
while (low <= high) {
val middle = (low + high) / 2
if ((ip >= lines(middle).split("\\|")(2).toLong) && (ip <= lines(middle).split("\\|")(3).toLong))
return middle
if (ip < lines(middle).split("\\|")(2).toLong)
high = middle - 1
else {
low = middle + 1
}
}
-1
}
def main(args: Array[String]) {
val ip = "192.168.12.224"
val ipNum = ip2Long(ip)
println(ipNum)
val lines = readData("E:ip.txt")//返回的是一个Array数组
val index = binarySearch(lines, ipNum)//返回ip对应区间段的索引值
print(lines(index))
}
}