phl0w
phl0w

Reputation: 17

A programming assignment, combining multiple files into one if they match names. FileWriter/BufferedWriter

Alright, so I'm supposed to do an assignment:

Say you have chat logs sorted like this:

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

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).

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.

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<String, File[]> files = new HashMap<String, File[]>();
private static final HashMap<File, File[]> filesToWrite = new HashMap<File, File[]>();

public static void main(String... args) {
    if (ROOT_FOLDER.exists()) {
        for (final File f : ROOT_FOLDER.listFiles()) {
            if (f.isDirectory() && 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<File> tempFiles = new ArrayList<File>();
                        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<String, File[]> 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<File, File[]> 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") && !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();
    }
}
}

Thanks alot!

Upvotes: 0

Views: 649

Answers (1)

Zac
Zac

Reputation: 2211

You have to close the buffered writer:

after ..

 bw = new BufferedWriter(new FileWriter(es.getKey()));
 in.close();
 new File(es.getKey() + ".temp").delete();
 bw.write(sb.toString());

add

 bw.close();

If the buffered writer is not closed the changes will not be saved, this explains the " (it forgets text) " you mentioned.

Upvotes: 1

Related Questions