0

さて、私は割り当てを行うことになっています:

次のようにソートされたチャット ログがあるとします。

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

システム 0 は私の最初のシステムで、システム 1 と 2 は他のコンピューターです。System-0/#channel を System-1/#channel ログ ファイルとマージするにはどうすればよいですか? 私はすでにそれらを取得する方法を理解していますが、BufferedWriter はランダムに書き込みを停止し (テキストを忘れます)、1 つのファイルに対してのみ機能します (つまり、異なるディレクトリに 2 つの重複ログ ファイルがあり、最初の重複ログ ファイルのみを処理します)。異なるディレクトリにあります)。

私の英語で申し訳ありません。私はネイティブ スピーカーではありませんが、私の言いたいことを理解していただければ幸いです。これが私がこれまでに持っているものです。私はまた、あらゆる改善を受け入れています。

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();
    }
}
}

どうもありがとう!

4

1 に答える 1

1

バッファされたライターを閉じる必要があります。

後 ..

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

追加

 bw.close();

バッファされたライターが閉じられていない場合、変更は保存されません。これは、あなたが言及した「(テキストを忘れる)」を説明しています。

于 2012-07-31T18:20:23.577 に答える