import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List; public class Fileinfo extends Object {
private FileWriter writer;
private BufferedWriter bw; private FileReader reader;
private BufferedReader br; private FileReadCallback callbackfun; public Fileinfo() {
super();
} public Fileinfo(FileReadCallback callbackfun) {
super();
this.callbackfun = callbackfun;
} public void setMyCallback(FileReadCallback callbackfun) {
this.callbackfun = callbackfun;
} public boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
} public boolean IsFileExist(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
flag = true;
}
return flag;
} public boolean CheckAndCreateFile(String filepath) throws IOException {
boolean flag;
File file = new File(filepath);
if (!file.isFile()) {
file.createNewFile();
}
return IsFileExist(filepath);
} public void Writeindex(String filepath, String value) {
try {
if (writer == null) {
CheckAndCreateFile(filepath);
writer = new FileWriter(filepath, false);
}
if (bw == null)
bw = new BufferedWriter(writer); writer = new FileWriter(filepath, false);
bw = new BufferedWriter(writer);
bw.write(value); bw.flush();
bw.close();
// System.gc(); } catch (IOException e) {
e.printStackTrace();
}
} public int Readlogindex(String filepath) {
int result = 0;
if (!IsFileExist(filepath)) {
return result;
} try {
if (reader == null)
reader = new FileReader(filepath); if (br == null)
br = new BufferedReader(reader); try {
String str = null;
if ((str = br.readLine()) != null) {
result = Integer.parseInt(str);
}
} finally {
// reader.close();
// br.close();
}
} catch (IOException e) {
e.printStackTrace();
} return result;
} public void Write(String filepath, boolean append, List<String> datalist) {
try {
if (writer == null) {
CheckAndCreateFile(filepath);
writer = new FileWriter(filepath, append);
}
if (bw == null)
bw = new BufferedWriter(writer); for (String index : datalist) {
bw.write(index);
bw.newLine();
}
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
} public void Write(String filepath, boolean append, String value) {
try {
if (writer == null) {
CheckAndCreateFile(filepath);
writer = new FileWriter(filepath, true);
}
if (bw == null)
bw = new BufferedWriter(writer); bw.write(value);
bw.newLine();
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
} public void Read(String filepath) {
try {
FileReader reader = new FileReader(filepath);
BufferedReader br = new BufferedReader(reader); String str = null;
while ((str = br.readLine()) != null) {
if (callbackfun != null) {
callbackfun.read(str);
}
}
} catch (IOException e) {
e.printStackTrace();
}
} protected void finalize() throws Throwable {
super.finalize();
System.out.println("finalize"); if (writer != null)
writer.close(); if (bw != null)
bw.close(); if (reader != null)
reader.close(); if (br != null)
br.close();
}
}
import java.util.ArrayList;
import java.util.List; import com.chexun.project.unit.*; public class IpMerge implements FileReadCallback {
class Ipdata {
long start;
long end;
String area; public long getStart() {
return start;
} public void setStart(long start) {
this.start = start;
} public long getEnd() {
return end;
} public void setEnd(long end) {
this.end = end;
} public String getArea() {
return area;
} public void setArea(String area) {
this.area = area;
} public Ipdata(String str) {
super();
parse(str);
} @Override
public String toString() {
return start + ", " + end + ", " + area;
} private void parse(String str){
String[] tmp = str.split(",");
if (3 == tmp.length){
start = Long.parseLong(tmp[0]);
end = Long.parseLong(tmp[1]);
area = tmp[2];
}
}
} private final List<String> iplist = new ArrayList<String>();
private Ipdata lastipdata;
long sum; public void read(String str) {
sum++;
Ipdata ipdata = new Ipdata(str);
if (lastipdata != null ){
if(ipdata.area != null && ipdata.area.equals(lastipdata.area)){
if (1 == (ipdata.start - lastipdata.end)) {
lastipdata.setEnd(ipdata.end);
}
}
else{
iplist.add(lastipdata.toString());
lastipdata = null;
}
}
else
lastipdata = ipdata;
} private void run(){
String dir = System.getProperty("user.dir");
String path = dir + "/resources/iptables.txt";
String outpathpath = dir + "/resources/iptablesnew.txt";
String path1 = "src/main/java/com.chexun.project.IpMerge/resources/iptables.txt";
Fileinfo fileinfo = new Fileinfo(this);
fileinfo.Read(path);
fileinfo.Write(outpathpath, false, iplist);
fileinfo =null;
System.gc();
} public static void main(String[] args) {
IpMerge ipmerge = new IpMerge();
ipmerge.run();
}
}

  

  

public interface FileReadCallback {
void read(String Str);
}

  

最新文章

  1. JavaScript与PHP中正则
  2. JSP连接数据库
  3. combobox 属性、事件、方法
  4. Android Framework层Power键关机流程(二,关机流程)
  5. Codeforces Round #368 (Div. 2) A. Brain&#39;s Photos (水题)
  6. TCL语言笔记:TCL练习二
  7. php结合jquery异步上传图片(ajaxSubmit)
  8. wireshark 抓包分析 TCPIP协议的握手
  9. Problem B: Ternarian Weights
  10. 九月 26, 2017 10:18:14 上午 com.sun.jersey.server.impl.application.RootResourceUriRules &lt;init&gt; 严重: The ResourceConfig instance does not contain any root resource classes.
  11. const类型变量的详细解读
  12. linkin大话面向对象--闭包和回调
  13. 终极解决方案:java.security.cert.CertificateException: Certificates does not conform to algorithm constraints
  14. hihoCoder 1044 : 状态压缩·一 状压dp
  15. ●SPOJ 1811 Longest Common Substring
  16. Java线程池(待续)
  17. 使用VBScript 脚本 开机播放声音
  18. (01背包 dp)P1049 装箱问题 洛谷
  19. [转]获取app的内部储存路径
  20. The Little Prince-12/09

热门文章

  1. 北京Uber优步司机奖励政策(4月6日)
  2. 天津市人民优步Uber司机奖励政策(8.31-9.6)
  3. 问题集 - console.log在IE下不可用
  4. Elastic stack ——X-Pack安装
  5. create-react-app react-redux项目 配置模块热更新hmr
  6. cf#516B. Equations of Mathematical Magic(二进制,位运算)
  7. hdu2037今年暑假不AC(贪心,活动安排问题)
  8. 使用.net 更新word目录
  9. Linux命令应用大词典-第2章 获取帮助
  10. informix如何查询第一条记录