从多个文件中读取并使用多线程将它们写入文件文件序列

时间:2014-10-13 18:23:16

标签: java multithreading

我想从多个文件中读取并使用多线程将它们写入单个文件。我做了一些代码,它们完全正确地读取文件内容并将它们写入另一个文件。但我的问题是数据不顺序。就像读取f1并写入输出文件一样,然后读取f2并写入输出文件,依此类推。看看我的代码

public MultiFileReadingThread(BufferedWriter writer, BufferedReader reader, String name, java.util.List<File> files){
        this.writer = writer;
        this.reader = reader;
        this.name = name;
        this.files = files;
    }

    @Override
    public void run() {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss:ms");
        System.out.println("Start Time : " + name + formatter.format(System.currentTimeMillis()));
//      readFile();
        if(i == 1){

        }
        writeIntoSharedBufferedWriter();
        System.out.println("End Time : " + name +  formatter.format(System.currentTimeMillis()));
    }

public void writeIntoBufferedWriter(){

        for(File f : files){
            try{
            BufferedReader r = new BufferedReader(new FileReader(f));
            String line = null;
            while((line = r.readLine()) != null){
                writer.write(line);
                writer.write("\n");
            }
            } catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }

运行线程的代码。他们有五个不同的任务和工人。这个代码给了我但不正确。比如f1,f2,f3

BufferedReader r1 = new BufferedReader(new FileReader(new File("/home/naveen/f1")));
        BufferedReader r2 = new BufferedReader(new FileReader(new File("/home/naveen/f2")));
        BufferedReader r3 = new BufferedReader(new FileReader(new File("/home/naveen/f3")));
        BufferedReader r4 = new BufferedReader(new FileReader(new File("/home/naveen/f4")));
        BufferedReader r5 = new BufferedReader(new FileReader(new File("/home/naveen/f5")));

        BufferedWriter writer = new BufferedWriter(new FileWriter(new File("/home/naveen/write")));
        Runnable task1 = new MultiFileReadingThread(null, writer, r1, "f");
        Runnable task2 = new MultiFileReadingThread(null, writer, r2, "s");
        Runnable task3 = new MultiFileReadingThread(null, writer, r3, "t");
        Runnable task4 = new MultiFileReadingThread(null, writer, r4, "fu");
        Runnable task5 = new MultiFileReadingThread(null, writer, r5, "fi");

        Thread wroker1 = new Thread(task1, "w1");
        Thread wroker2 = new Thread(task2, "w2");
        Thread wroker3 = new Thread(task3, "w3");
        Thread wroker4 = new Thread(task4, "w4");
        Thread wroker5 = new Thread(task5, "w5");

有人可以帮助实现这一目标。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

更好的方法是使用生产者消费模式。生产者线程将读取文件并通过将其包装在某个类中将内容发送给消费者。将组合文件写入磁盘。

在消费者方面,您可以使用优先级队列,让生产者设置优先级。

答案 1 :(得分:0)

嗯,这里有很多不必要的代码重复。

MultiFileReadingThread的每个实例都应该有自己的读者和编写器,永远不会从线程外部访问。看起来你有多个线程使用同一个编写器,在这种情况下,这不会很好。

试试这个:

public MultiFileReadingThread(String name, List<File> inputFiles, File outputFile) {
    this.writer = new BufferedWriter(new FileWriter(outputFile));
    this.name = name;
    this.files = inputFiles;
}

@Override
public void run() {
    SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss:ms");
    System.out.println("Start Time : " + name + formatter.format(System.currentTimeMillis()));
    //readFile();
    if(i == 1){

    }
    writeIntoSharedBufferedWriter();
    System.out.println("End Time : " + name +  formatter.format(System.currentTimeMillis()));
}

public void writeIntoBufferedWriter(){
    for(File f : files){
        try{
        BufferedReader r = new BufferedReader(new FileReader(f));
        String line = null;
        while((line = r.readLine()) != null){
            writer.write(line);
            writer.write("\n");
        }
        } catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

不要忘记您还需要关闭BufferedReader和BufferedWriter。

线程化可能很困难,遗憾的是,没有单一的教程或资源可以提供您需要的所有信息。我建议多次阅读你能找到的每一个。在您对该主题有很强的把握之前尝试实现多线程可能会导致很多问题。有时线程问题直到应用程序生命周期的后期才会变得明显,因为需要进行重大更改才能纠正它们。