标签:blog io ar os java sp for 文件 div
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); }
标签:blog io ar os java sp for 文件 div
原文地址:http://www.cnblogs.com/chengxin1982/p/4089725.html