A programming assignment, combining multiple files into one if they match names. FileWriter/BufferedWriter
<p>Alright, so I'm supposed to do an assignment:</p>

<p>Say you have chat logs sorted like this:</p>

<pre><code>System-0/Server-#channel.log
System-0/Server-#channel1.log
System-0/Server-#channel2.log
System-1/Server-#channel.log
System-1/Server-#channel2.log
System-2/Server-#channel3.log
</code></pre>

<p>System-0 being my first system, system 1 and 2 being other computers. How would I go on about merging System-0/#channel with System-1/#channel log files? I've already figured out how to get them, however, BufferedWriter randomly stops writing (it forgets text) and it only works for 1 file (so say there are 2 duplicate log files in different directories it will only process the first duplicate log file in different directories).</p>

<p>Sorry for my English, I'm not a native speaker but I hope you understood what I meant. Here's what I have so far: I'm also open to any improvements.</p>

<pre><code>import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

public class MergeV2 {

private static final Logger log = Logger.getLogger(MergeV2.class.getName());
private static final File ROOT_FOLDER = new File(System.getProperty("user.home") + (System.getProperty("os.name").contains("Windows") ? "/AppData/Roaming/X-Chat 2/xchatlogs" : "/.xchat2/xchatlogs"));
private static final HashMap&lt;String, File[]&gt; files = new HashMap&lt;String, File[]&gt;();
private static final HashMap&lt;File, File[]&gt; filesToWrite = new HashMap&lt;File, File[]&gt;();

public static void main(String... args) {
    if (ROOT_FOLDER.exists()) {
        for (final File f : ROOT_FOLDER.listFiles()) {
            if (f.isDirectory() &amp;&amp; f.getName().contains("System")) { //mandatory check
                for (final File sub : f.listFiles()) {
                    String channelName = sub.getName().split("#")[1].replaceAll(".log", "");
                    if (files.containsKey(channelName)) {
                        ArrayList&lt;File&gt; tempFiles = new ArrayList&lt;File&gt;();
                        for (File t : files.get(channelName)) {
                            tempFiles.add(t);
                        }
                        tempFiles.add(sub);
                        File[] array = new File[tempFiles.size()];
                        array = tempFiles.toArray(array);
                        files.put(channelName, array);
                    } else {
                        files.put(channelName, new File[]{sub});
                    }
                }
            }
        }
    } else {
        log.info("No log folder detected.");
    }
    String channel;
    File f, ftemp;
    for (Map.Entry&lt;String, File[]&gt; es : files.entrySet()) {
        channel = "#" + es.getKey();
        f = new File(ROOT_FOLDER.getAbsolutePath() + "/merged-" + channel + ".log");
        ftemp = new File(f.getAbsolutePath() + ".temp");
        if (f.exists()) {
            f.delete();
            ftemp.delete();
        }
        try {
            f.createNewFile();
            ftemp.createNewFile();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        filesToWrite.put(f, es.getValue());
    }
    try {
        FileWriter fw = null;
        BufferedWriter bw = null;
        BufferedReader in = null;
        for (Map.Entry&lt;File, File[]&gt; es : filesToWrite.entrySet()) {
            File temp = new File(es.getKey() + ".temp");
            bw = new BufferedWriter(new FileWriter(temp));
            for (File t : es.getValue()) {
                bw.write(readFile(t.getAbsolutePath()));
            }
            in = new BufferedReader(new FileReader(new File(es.getKey() + ".temp")));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null) {
                if (!line.contains("**** LOGGEN") &amp;&amp; !line.contains("**** LOGGING")) {
                    sb.append(line);
                }
            }
            bw = new BufferedWriter(new FileWriter(es.getKey()));
            in.close();
            new File(es.getKey() + ".temp").delete();
            bw.write(sb.toString());
        }
    } catch (IOException e) {

    }
}

private static String readFile(String path) throws IOException {
    System.out.println("reading " + path);
    FileInputStream stream = new FileInputStream(new File(path));
    try {
        FileChannel chan = stream.getChannel();
        MappedByteBuffer mbb = chan.map(FileChannel.MapMode.READ_ONLY, 0, chan.size());
        return Charset.defaultCharset().decode(mbb).toString();
    } finally {
        stream.close();
    }
}
}
</code></pre>

<p>Thanks alot!</p>
