Java 关于文件读取速度问题,求助,谢谢啦
publicclassChangeIp2Address{staticChangeIp2Addresschangeip2address=newChangeIp2Addres...
public class ChangeIp2Address {
static ChangeIp2Address changeip2address = new ChangeIp2Address();
public static String[] utilIpData(String ip) throws FileNotFoundException{
String[] ipData = new String[9];
String temp = null;
String address = null;
String Country = null;
String Province = null;
String City = null;
String Region = null;
String Local = null;
String[] ipResult = new String[5];
long ip2Long = changeip2address.ipToLong(ip);
File file = new File("E://12.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
try{
while((temp=reader.readLine())!=null){
ipData = temp.split(",");
long StartIpNum = Long.parseLong(ipData[1]);
long EndIpNum = Long.parseLong(ipData[3]);
if(StartIpNum<=ip2Long&&ip2Long<=EndIpNum){
Country = ipData[5];
Province = ipData[6];
City = ipData[7];
Region = ipData[8];
Local = ipData[9];
}
ipResult[0] = Country;
ipResult[1] = Province;
ipResult[2] = City;
ipResult[3] = Region;
ipResult[4] = Local;
address = Country+","+Province+","+City+","+Region+","+Local;
}
}catch(IOException e){
e.printStackTrace();
}
return ipResult;
}
//传递string 类型ip返回对应国家
public static String getIpCountry(String ip) throws FileNotFoundException{
String ipCountry = changeip2address.utilIpData(ip)[0];
return ipCountry;
}
}
上面是我写的方法,每次执行getIPCity都要去读12.txt文件执行速度也不快,有什么办法提高执行效率,节省时间啊,真心求助。 展开
static ChangeIp2Address changeip2address = new ChangeIp2Address();
public static String[] utilIpData(String ip) throws FileNotFoundException{
String[] ipData = new String[9];
String temp = null;
String address = null;
String Country = null;
String Province = null;
String City = null;
String Region = null;
String Local = null;
String[] ipResult = new String[5];
long ip2Long = changeip2address.ipToLong(ip);
File file = new File("E://12.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
try{
while((temp=reader.readLine())!=null){
ipData = temp.split(",");
long StartIpNum = Long.parseLong(ipData[1]);
long EndIpNum = Long.parseLong(ipData[3]);
if(StartIpNum<=ip2Long&&ip2Long<=EndIpNum){
Country = ipData[5];
Province = ipData[6];
City = ipData[7];
Region = ipData[8];
Local = ipData[9];
}
ipResult[0] = Country;
ipResult[1] = Province;
ipResult[2] = City;
ipResult[3] = Region;
ipResult[4] = Local;
address = Country+","+Province+","+City+","+Region+","+Local;
}
}catch(IOException e){
e.printStackTrace();
}
return ipResult;
}
//传递string 类型ip返回对应国家
public static String getIpCountry(String ip) throws FileNotFoundException{
String ipCountry = changeip2address.utilIpData(ip)[0];
return ipCountry;
}
}
上面是我写的方法,每次执行getIPCity都要去读12.txt文件执行速度也不快,有什么办法提高执行效率,节省时间啊,真心求助。 展开
展开全部
/**
* ip条目实体类
*/
public class IpEntry {
String country, province, city, region, local;
long start = 0, end = 0;
public String getCountry() {
return country;
}
public String getProvince() {
return province;
}
public String getCity() {
return city;
}
public String getRegion() {
return region;
}
public String getLocal() {
return local;
}
/**
* 接受字符串初始化属性
* @param text
*/
public IpEntry(String text) {
String fields[] = text.split(",");
start = Long.parseLong(fields[1]);
end = Long.parseLong(fields[3]);
country = fields[5];
province = fields[6];
city = fields[7];
region = fields[8];
local = fields[9];
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* ip地址表类
*/
public class IpTable {
List<IpEntry> table;
//读取文件写入, 逐行构造IpEntry, 写入list
public IpTable(String fileName) {
BufferedReader rd = null;
String line;
table = new LinkedList<IpEntry>();
try {
rd = new BufferedReader(new FileReader(fileName));
while (true) {
line = rd.readLine();
if (null == line)
break;
table.add(new IpEntry(line));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//IO资源必须在finally中关闭
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//匹配第一条
public IpEntry matchFirst(long ip) {
for (IpEntry entry : table) {
if (entry.start <= ip && entry.end >= ip)
return entry;
}
return null;
}
//匹配所有
public IpEntry[] matchAll(long ip) {
List<IpEntry> list = new ArrayList<IpEntry>();
for (IpEntry entry : table) {
if (entry.start <= ip && entry.end >= ip)
list.add(entry);
}
return list.toArray(new IpEntry[list.size()]);
}
//静态方法
static final String IP_TABLE_FILE_NAME = "E://12.txt";
static IpTable instance = null;
public static IpEntry match(long ip) {
//仅在第一次调用时,初始化静态实例读取文件
if (instance == null)
instance = new IpTable(IP_TABLE_FILE_NAME);
return instance.matchFirst(ip);
}
//你要的方法
public static String getIpCountry(String ip) {
return match(ipToLong(ip)).getCountry(); //你自己的ipToLong方法
}
//模拟测试
public static void main(String[] args) {
String ip[] = {
"192.168.1.1",
//...
"220.10.10.135"
};
for (int i = 0; i < ip.length; i++) {
System.out.println(IpTable.getIpCountry(ip[i]));
}
}
}
可能你一下转不过来,根据你代码里给的结构给你写了个完整的示例。
主要就是把文件内容读取后格式化放在内存对象中,让后只要在对象中查找匹配,就不用再去读取文件了。关于性能优化的话,因为每次都是顺序迭代查找,所以用了LinkedList,其他的话暂时也想不出什么可以优化的地方了
展开全部
/**
* 可以将File中的字符串存放到List中,这样可以增加效率。
* @param file File路径
* @return list 返回一个List
* @throws IOException
*/
private List<String> file2String(File file) throws IOException{
List<String> list = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader(file));
String temp = null;
while((temp = reader.readLine())!=null ){
list.add(temp);
}
reader.close();
return list;
}
这样的话每次执行getIPCity就可以直接从List中读取字符串了。
更多追问追答
追问
你这是把文件放到list中?问题是我怎么根据给定Ip来定位city呢,求解
追答
和你用IO从文件读取的方式是一样的,不过首先一次性用IO流将文件里面的数据读取出来,存放到List中,这样你每次操作数据就是从List中,而不是从IO流中。其实就是进行了一次封装,代码传不上来,超过字数限制。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询