|
1 /* |
|
2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 |
|
27 // Append files to one another without duplicating any lines. |
|
28 |
|
29 import java.io.BufferedReader; |
|
30 import java.io.FileReader; |
|
31 import java.util.HashMap; |
|
32 |
|
33 public class Combine { |
|
34 |
|
35 private static HashMap map = new HashMap(10007); |
|
36 |
|
37 private static void appendFile(String fileName, boolean keep) { |
|
38 try { |
|
39 BufferedReader br = new BufferedReader(new FileReader(fileName)); |
|
40 |
|
41 // Read a line at a time. If the line does not appear in the |
|
42 // hashmap, print it and add it to the hashmap, so that it will |
|
43 // not be repeated. |
|
44 |
|
45 lineLoop: |
|
46 while (true) { |
|
47 String line = br.readLine(); |
|
48 if (line == null) |
|
49 break; |
|
50 if (keep || !map.containsKey(line)) { |
|
51 System.out.println(line); |
|
52 map.put(line,line); |
|
53 } |
|
54 } |
|
55 br.close(); |
|
56 } catch (Exception e) { |
|
57 e.printStackTrace(); |
|
58 System.exit(1); |
|
59 } |
|
60 } |
|
61 |
|
62 |
|
63 public static void main(String[] args) { |
|
64 |
|
65 if (args.length < 2) { |
|
66 System.err.println("Usage: java Combine file1 file2 ..."); |
|
67 System.exit(2); |
|
68 } |
|
69 |
|
70 for (int i = 0; i < args.length; ++i) |
|
71 appendFile(args[i], i == 0); |
|
72 } |
|
73 } |