Skip to content

Instantly share code, notes, and snippets.

@dangnhdev
Created November 25, 2015 10:34
Show Gist options
  • Save dangnhdev/c8a859017c5a3caf0347 to your computer and use it in GitHub Desktop.
Save dangnhdev/c8a859017c5a3caf0347 to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.processor;
import org.common.AppLogger;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author 404NotFound
*/
public class FileWriter implements Runnable {
private final String filePath;
private final BlockingQueue<String> data;
private final File file;
public FileWriter(String filePath, BlockingQueue<String> data) {
this.filePath = filePath;
this.data = data;
this.file = new File(filePath);
}
@Override
public void run() {
while (true) {
String s = "";
StringBuilder dataBuffer = new StringBuilder();
while ((s = data.poll()) != null){
dataBuffer.append(s).append("\n");
}
try (BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(file, true))) {
AppLogger.getInstance().debug("Writing data to file: " + filePath);
writer.write(dataBuffer.toString());
Thread.sleep(5000);
} catch (IOException | InterruptedException ex) {
AppLogger.getInstance().error(ex.getMessage());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment