呃。。。。代码编辑坏了- -! /* * @(#)Main.java * Author: 88250 , http://blog.csdn.net/DL88250 * Created on May 13, 2008, 4:11:44 PM * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package checkthesame; 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.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; /** * Check the same record in a file. *
* Every record in data file is a random serial, like the followings:
* // data file * 1902323484354370234844
* 1928473090393719374
* ....
*
java.util.ArrayList
store them. * @see #records */ public static void readRecords() { System.out.println("Get starting read records...."); try { BufferedReader reader = new BufferedReader( new FileReader("data.txt")); String aLine; while ((aLine = reader.readLine()) != null) { records.add(aLine); } reader.close(); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("The amount of records: " + records.size()); } /** * Create some records for test. */ public static void createTestRecords() { File file = new File("data.txt"); if (file.exists()) { file.delete(); } try { BufferedWriter writer = new BufferedWriter(new FileWriter("data.txt")); Random random = new Random(); byte[] bytes = new byte[16]; for (int i = 0; i < 1000000; i++) { StringBuffer aLine; random.nextBytes(bytes); aLine = new StringBuffer(); for (int j = 0; j < 16; j++) { aLine.append((int) bytes[j]); } // System.out.println(aLine); writer.write(aLine.toString()); //System.out.println(); writer.newLine(); } writer.close(); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } /** * Main program entry. * @param args the command line arguments, should be
null
*/ public static void main(String[] args) { createTestRecords(); readRecords(); displayRecords(10); checkTheSame(); } /** * Check the same data records in {@link #records}. */ public static void checkTheSame() { sortTheRecords(); // sort them // displayRecords(10); for (int i = 0; i < records.size() - 1; i++) { String record1 = records.get(i); String record2 = records.get(i + 1); if (record1.equals(record2)) { List equalities = new ArrayList(); equalities.add(record1); equalities.add(record2); statistics.add(equalities); } } displayStats(); } /** * Display the data records in console. * @param amount display amount, start from {@link #records}'s beginning */ public static void displayRecords(int amount) { if (amount < 0 || amount > records.size()) { System.out.println("The specified amount exceeds the Data records" + "size!"); } System.out.println("Display: "); for (int i = 0; i < amount; i++) { System.out.println(records.get(i)); } System.out.println(); } /** * Display the statistic results in console. */ private static void displayStats() { System.out.println("Statistics: "); System.out.println("A amount of the same data records: " + statistics. size()); for (List aEqualities : statistics) { System.out.println(aEqualities.get(0) + " occurs " + aEqualities. size()); } } /** * Using {@link java.util.Collections#sort(java.util.List)} to sort the * data records. */ @SuppressWarnings("unchecked") private static void sortTheRecords() { java.util.Collections.sort(records, new Comparator() { @Override public int compare(Object o1, Object o2) { String r1 = (String) o1; String r2 = (String) o2; return r1.compareTo(r2); } }); } }