BAEL-36 BAEL-282 Getting file size using Java and apache commons IO api. Also grep in Java (#890)

* BAL-36 File size api in java and apache commons IO

* BAEL-36 Getting file size using Java and commons IO api.

* BAEL-282 Added support for grep functionality.

* BAEL-282 Rename the text file extension.
This commit is contained in:
Muhammed Almas 2016-12-17 18:49:13 +05:30 committed by Eugen
parent 5faf49eae2
commit b4ac6c6ec5
4 changed files with 178826 additions and 0 deletions

View File

@ -51,6 +51,18 @@
<artifactId>bcprov-jdk15on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>org.unix4j</groupId>
<artifactId>unix4j-command</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>com.googlecode.grep4j</groupId>
<artifactId>grep4j</artifactId>
<version>1.8.7</version>
</dependency>
<!-- web -->
<!-- marshalling -->

View File

@ -0,0 +1,65 @@
package com.baeldung.java8;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
public class JavaFileSizeUnitTest {
private static final long EXPECTED_FILE_SIZE_IN_BYTES = 11;
private String filePath;
@Before
public void init() {
final String separator = File.separator;
filePath = String.join(separator, new String[] {"src", "test", "resources", "testFolder", "sample_file_1.in"});
}
@Test
public void whenGetFileSize_thenCorrect() {
final File file = new File(filePath);
final long size = getFileSize(file);
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, size);
}
@Test
public void whenGetFileSizeUsingNioApi_thenCorrect() throws IOException {
final Path path = Paths.get(this.filePath);
final FileChannel fileChannel = FileChannel.open(path);
final long fileSize = fileChannel.size();
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, fileSize);
}
@Test
public void whenGetFileSizeUsingApacheCommonsIO_thenCorrect() {
final File file = new File(filePath);
final long size = FileUtils.sizeOf(file);
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, size);
}
@Test
public void whenGetReadableFileSize_thenCorrect() {
final File file = new File(filePath);
final long size = getFileSize(file);
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES + " bytes", FileUtils.byteCountToDisplaySize(size));
}
private long getFileSize(final File file) {
final long length = file.length();
return length;
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.java8.unix.grep;
import java.io.File;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.unix4j.Unix4j;
import static org.unix4j.Unix4j.*;
import static org.junit.Assert.assertEquals;
import org.unix4j.line.Line;
import static org.unix4j.unix.Grep.*;
import static org.unix4j.unix.cut.CutOption.*;
public class GrepWithUnix4JTest {
private File fileToGrep;
@Before
public void init() {
final String separator = File.separator;
final String filePath = String.join(separator, new String[] {"src", "test", "resources", "dictionary.in"});
fileToGrep = new File(filePath);
}
@Test
public void whenGrepWithSimpleString_thenCorrect() {
int expectedLineCount = 4;
//grep "NINETEEN" dictionary.txt
List<Line> lines = Unix4j.grep("NINETEEN", fileToGrep).toLineList();
assertEquals(expectedLineCount, lines.size());
}
@Test
public void whenInverseGrepWithSimpleString_thenCorrect() {
int expectedLineCount = 178687;
//grep -v "NINETEEN" dictionary.txt
List<Line> lines = grep(Options.v, "NINETEEN", fileToGrep).
toLineList();
assertEquals(expectedLineCount, lines.size());
}
@Test
public void whenGrepWithRegex_thenCorrect() {
int expectedLineCount = 151;
//grep -c ".*?NINE.*?" dictionary.txt
String patternCount = grep(Options.c, ".*?NINE.*?", fileToGrep).
cut(fields, ":", 1).toStringResult();
assertEquals(expectedLineCount, Integer.parseInt(patternCount));
}
}

File diff suppressed because it is too large Load Diff