Skip to content

Instantly share code, notes, and snippets.

@donhenton
Last active December 14, 2017 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donhenton/81775eefedd135f3ce94839d6d7bd3f2 to your computer and use it in GitHub Desktop.
Save donhenton/81775eefedd135f3ce94839d6d7bd3f2 to your computer and use it in GitHub Desktop.
Java 8 File Splitter Demonstrating Files and Path Usage
package com.dhenton9000.anttask;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;
/**
Demonstration of file splitting for drools drl files Large file with multiple when then end blocks
cuts the file at a certain size on the 'end' line. Demonstates file handling via Java 8
constructs
*/
public class FileSplitter {
private static final Logger LOG = LoggerFactory.getLogger(FileSplitter.class);
public static void main(String[] args) {
String destinationDir = "./target/split-result/";
String sourceFile = "src/main/resources/samples/source.drl";
try {
(new FileSplitter()).doFileSplit(destinationDir, sourceFile);
} catch (Exception err) {
LOG.error("Main error " + err.getClass().getName() + " " + err.getMessage());
}
}
public void makeDirectory(String splitDir) throws IOException {
Set<PosixFilePermission> perms = new HashSet<>();
Path dirPath = Paths.get(splitDir);
// add permission as rw-r--r-- 644
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_READ);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
FileAttribute<Set<PosixFilePermission>> fileAttributes
= PosixFilePermissions.asFileAttribute(perms);
if (Files.exists(dirPath, LinkOption.NOFOLLOW_LINKS)) {
Files.walk(dirPath, FileVisitOption.FOLLOW_LINKS)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
// .peek(System.out::println)
.forEach(File::delete);
}
Files.createDirectory(dirPath, fileAttributes);
}
private void doFileSplit(String destinationDir, String srcFile) throws IOException {
makeDirectory(destinationDir);
Path destPath = Paths.get(destinationDir);
Path srcPath = Paths.get(srcFile);
final LineProcessor lineProcessor = new LineProcessor();
lineProcessor.setSrcPath(srcPath);
lineProcessor.setDestPath(destPath);
lineProcessor.setDestFileStem("subfile%s.drl");
try (Stream<String> stream = Files.lines(srcPath)) {
stream.forEach(lineProcessor);
lineProcessor.flushFinalBuffer();
} catch (IOException err) {
LOG.error("IO Error " + err.getMessage());
}
}
public class LineProcessor implements Consumer {
private final Logger LOG
= LoggerFactory.getLogger(LineProcessor.class);
private Path srcPath;
private Path destPath;
private long lineCount;
private static final int FILE_COUNT = 10;
private long linesPerFile;
private String destFileStem;
private int fileIdx = 1;
private StringBuilder newBuffer = new StringBuilder();
private final StandardOpenOption oOption
= StandardOpenOption.CREATE_NEW;
public LineProcessor() {
}
int lineCounter = 0;
int accumCount = 0;
// boolean foundRuleStart = false;
@Override
public void accept(Object t) {
String line = (String) t;
// if (isRuleLine(line)) {
// foundRuleStart = true;
// }
if (isEndLine(line)) {
// foundRuleStart = false;
if (this.accumCount > this.linesPerFile) {
writeOutLine(line);
try {
Files.write(this.getCurrentWriteFile(),
this.newBuffer.toString().getBytes(),
oOption);
this.newBuffer = new StringBuilder();
this.fileIdx++;
this.accumCount = 0;
} catch (IOException ex) {
throw new RuntimeException("write problem "
+ ex.getMessage());
}
} else {
writeOutLine(line);
}
} else {
writeOutLine(line);
}
}
private void writeOutLine(String line) {
newBuffer.append(line);
newBuffer.append("\n");
this.accumCount++;
}
public void flushFinalBuffer() throws IOException {
if (this.newBuffer.length() > 0) {
Files.write(this.getCurrentWriteFile(),
this.newBuffer.toString().getBytes(),
oOption);
}
}
private Path getCurrentWriteFile() {
String currentFile = String.format("_%03d", this.fileIdx);
String currentFilePath
= String.format(this.getDestFileStem(), currentFile);
return this.getDestPath().resolve(currentFilePath);
}
// private boolean isRuleLine(String line) {
// return line.trim().indexOf("rule ") == 0;
// }
private boolean isEndLine(String line) {
return line.trim().equals("end");
}
/**
* @return the srcPath
*/
public Path getSrcPath() {
return srcPath;
}
/**
* @param srcPath the srcPath to set
*/
public void setSrcPath(Path srcPath) {
try {
this.srcPath = srcPath;
this.lineCount = Files.lines(getSrcPath()).count();
this.linesPerFile = Math.round((float) this.getLineCount() / FILE_COUNT);
} catch (IOException ex) {
throw new RuntimeException("issues with line count "
+ ex.getMessage());
}
}
/**
* @return the destPath
*/
public Path getDestPath() {
return destPath;
}
/**
* @param destPath the destPath to set
*/
public void setDestPath(Path destPath) {
this.destPath = destPath;
}
/**
* @return the lineCount
*/
public long getLineCount() {
return lineCount;
}
/**
* @return the destFileStem
*/
public String getDestFileStem() {
return destFileStem;
}
/**
* @param destFileStem the destFileStem to set
*/
public void setDestFileStem(String destFileStem) {
this.destFileStem = destFileStem;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment