Skip to content

Commit b4ac6c6

Browse files
eralmas7Eugen
authored andcommitted
BAEL-36 BAEL-282 Getting file size using Java and apache commons IO api. Also grep in Java (eugenp#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.
1 parent 5faf49e commit b4ac6c6

4 files changed

Lines changed: 178826 additions & 0 deletions

File tree

core-java/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@
5151
<artifactId>bcprov-jdk15on</artifactId>
5252
<version>${bouncycastle.version}</version>
5353
</dependency>
54+
55+
<dependency>
56+
<groupId>org.unix4j</groupId>
57+
<artifactId>unix4j-command</artifactId>
58+
<version>0.4</version>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>com.googlecode.grep4j</groupId>
63+
<artifactId>grep4j</artifactId>
64+
<version>1.8.7</version>
65+
</dependency>
5466
<!-- web -->
5567

5668
<!-- marshalling -->
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.java8;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.channels.FileChannel;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import org.apache.commons.io.FileUtils;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
14+
public class JavaFileSizeUnitTest {
15+
private static final long EXPECTED_FILE_SIZE_IN_BYTES = 11;
16+
private String filePath;
17+
18+
@Before
19+
public void init() {
20+
final String separator = File.separator;
21+
filePath = String.join(separator, new String[] {"src", "test", "resources", "testFolder", "sample_file_1.in"});
22+
}
23+
24+
@Test
25+
public void whenGetFileSize_thenCorrect() {
26+
final File file = new File(filePath);
27+
28+
final long size = getFileSize(file);
29+
30+
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, size);
31+
}
32+
33+
@Test
34+
public void whenGetFileSizeUsingNioApi_thenCorrect() throws IOException {
35+
final Path path = Paths.get(this.filePath);
36+
final FileChannel fileChannel = FileChannel.open(path);
37+
38+
final long fileSize = fileChannel.size();
39+
40+
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, fileSize);
41+
}
42+
43+
@Test
44+
public void whenGetFileSizeUsingApacheCommonsIO_thenCorrect() {
45+
final File file = new File(filePath);
46+
47+
final long size = FileUtils.sizeOf(file);
48+
49+
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, size);
50+
}
51+
52+
@Test
53+
public void whenGetReadableFileSize_thenCorrect() {
54+
final File file = new File(filePath);
55+
56+
final long size = getFileSize(file);
57+
58+
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES + " bytes", FileUtils.byteCountToDisplaySize(size));
59+
}
60+
61+
private long getFileSize(final File file) {
62+
final long length = file.length();
63+
return length;
64+
}
65+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.java8.unix.grep;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.unix4j.Unix4j;
9+
import static org.unix4j.Unix4j.*;
10+
import static org.junit.Assert.assertEquals;
11+
import org.unix4j.line.Line;
12+
import static org.unix4j.unix.Grep.*;
13+
import static org.unix4j.unix.cut.CutOption.*;
14+
15+
public class GrepWithUnix4JTest {
16+
17+
private File fileToGrep;
18+
19+
@Before
20+
public void init() {
21+
final String separator = File.separator;
22+
final String filePath = String.join(separator, new String[] {"src", "test", "resources", "dictionary.in"});
23+
fileToGrep = new File(filePath);
24+
}
25+
26+
@Test
27+
public void whenGrepWithSimpleString_thenCorrect() {
28+
int expectedLineCount = 4;
29+
30+
//grep "NINETEEN" dictionary.txt
31+
List<Line> lines = Unix4j.grep("NINETEEN", fileToGrep).toLineList();
32+
33+
assertEquals(expectedLineCount, lines.size());
34+
}
35+
36+
@Test
37+
public void whenInverseGrepWithSimpleString_thenCorrect() {
38+
int expectedLineCount = 178687;
39+
40+
//grep -v "NINETEEN" dictionary.txt
41+
List<Line> lines = grep(Options.v, "NINETEEN", fileToGrep).
42+
toLineList();
43+
44+
assertEquals(expectedLineCount, lines.size());
45+
}
46+
47+
48+
@Test
49+
public void whenGrepWithRegex_thenCorrect() {
50+
int expectedLineCount = 151;
51+
52+
//grep -c ".*?NINE.*?" dictionary.txt
53+
String patternCount = grep(Options.c, ".*?NINE.*?", fileToGrep).
54+
cut(fields, ":", 1).toStringResult();
55+
56+
assertEquals(expectedLineCount, Integer.parseInt(patternCount));
57+
}
58+
}

0 commit comments

Comments
 (0)