Skip to content

Instantly share code, notes, and snippets.

@hoanbka
Created October 3, 2016 16:14
Show Gist options
  • Save hoanbka/8b53e119348ccb469e258234d4a8dd75 to your computer and use it in GitHub Desktop.
Save hoanbka/8b53e119348ccb469e258234d4a8dd75 to your computer and use it in GitHub Desktop.
MessageFilter
package filterfile;
import java.io.*;
import java.util.concurrent.LinkedBlockingQueue;
/**
* Created by hoanbka on 03-Oct-16.
*/
public class FilterFile {
private static final File file = new File("D:\\Test\\fuck.txt");
private static final File resultFile = new File("result.txt");
private static LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
private static class ReadFile extends Thread{
@Override
public void run() {
try(BufferedReader reader= new BufferedReader(new FileReader(file))){
String line=null;
while ((line=reader.readLine())!=null){
if (line.contains("fuck")||(!line.startsWith("0") && !line.startsWith("84"))){
queue.offer(line);
}
}
queue.offer("End of file");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private static class WriteFile extends Thread{
@Override
public void run() {
try(BufferedWriter writer= new BufferedWriter(new FileWriter(resultFile))){
while (true){
if (!queue.isEmpty()){
String line= queue.poll();
if (line.equals("End of file")){
break;
}
writer.write(line);
writer.write("\n");
writer.flush();
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) {
new ReadFile().start();
new WriteFile().start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment