This solutions reacts both to inexistent files and general
IO problems:
File not found: Testdata/input.java
Two test cases deal both with readable and non-existing
files: and expected exceptions:
@Test
public void testReadFileOk() throws FileNotFoundException, IOException {
ReadFile.openStream("Testdata/input.txt"); // Existing file
}
@Test (expected=FileNotFoundException.class) // We expect this exception to be
// thrown.
public void testReadMissingFile() throws FileNotFoundException, IOException {
ReadFile.openStream("Testdata/input.java"); // Does not exist
}
Notice the second test which will only succeed if a
FileNotFoundException
is being thrown.
No. 188
A partial implementation of GNU UNIX
wc
Q:
In this exercise we will partly implement the (Gnu) UNIX
command line tool wc
(word count). Prior to starting this exercise you may want
to:
Execute wc
for sample text files like e.g. a Java source file of
similar:
goik >grep int BoundedIntegerStore.java | wc
12 76 516
The above output “12 76 516” tells us that
our file BoundedIntegerStore.java does
have 12 lines containing the string “int”.
A partial implementation shall offer all features being
mentioned in the introduction. The following steps are a proposal
for your implementation:
Write a method counting the number of words within a
given string. We assume words to be separated by at least one
white space character (space or \t).
Write some tests to assure correct behaviour.
Read input either from a list of files or from standard
input depending on the number of arguments to main(String[]
args):
If args.length == 0
assume to read from standard input.
if 0 < args.length
try to interpret the arguments as filenames.
Write a class TextFileStatistics
being able to and count characters, words and lines of a
single input file. Instances of this class may be initialized
from a BufferedReader.
new BufferedReader(new InputStreamReader(System.in))
Create an executable Jar archive and execute some
examples. The UNIX command cat
writes a file's content to standard output. This output may be
piped as input to your application as in cat
filename.txt | java -jar .../wc-1.0.jar.
A:
Maven module source code available at P/Sd1/Wc/wc.