diff --git a/Sd1/P/Wc/readFile/Testdata/input.txt b/Sd1/P/Wc/readFile/Testdata/input.txt new file mode 100644 index 0000000000000000000000000000000000000000..a3722d034f75fb5d8a2d2ca6fb40d2d1313f9f0f --- /dev/null +++ b/Sd1/P/Wc/readFile/Testdata/input.txt @@ -0,0 +1,8 @@ +<html> + <head> + <title>A simple HTML example</title> + </head> + <body> + <p>Some text ... </p> + </body> +</html> \ No newline at end of file diff --git a/Sd1/P/Wc/readFile/src/main/java/de/hdm_stuttgart/mi/sd1/readfile/ReadFile.java b/Sd1/P/Wc/readFile/src/main/java/de/hdm_stuttgart/mi/sd1/readfile/ReadFile.java index a912188537b373cb9422a0e170c8667d0189f18b..dfabc82ec55c0b133b3f4ec95531cfd582b199db 100644 --- a/Sd1/P/Wc/readFile/src/main/java/de/hdm_stuttgart/mi/sd1/readfile/ReadFile.java +++ b/Sd1/P/Wc/readFile/src/main/java/de/hdm_stuttgart/mi/sd1/readfile/ReadFile.java @@ -1,33 +1,23 @@ package de.hdm_stuttgart.mi.sd1.readfile; import java.io.BufferedReader; -import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; -import java.io.InputStreamReader; - /** * Reading a text file and writing all content * to standard output */ public class ReadFile { - + /** * @param args unused */ public static void main(String[] args) { - final String inputFileName = "pom.xml"; // serving as a sample input file - openStream(inputFileName); - } - - static void openStream(final String inputFileName) { + final String inputFileName = "Testdata/input.txt"; // serving as a sample input file try { - - final FileInputStream inputStream = new FileInputStream(inputFileName); - final InputStreamReader inputReader = new InputStreamReader(inputStream); - final BufferedReader inputBufferedReader = new BufferedReader(inputReader); - readStream(inputBufferedReader); + openStream(inputFileName); } catch (FileNotFoundException e) { System.err.println("File not found: " + inputFileName); } catch (IOException e) { @@ -35,7 +25,21 @@ public class ReadFile { System.err.println("Message was: " + e.getLocalizedMessage()); } } - + + /** + * Trying to read a given filename or -path + * + * @param inputFileName The intended file's pathname + * @throws FileNotFoundException inputFileName does not exist + * @throws IOException inputFileName cannot be read + */ + public static void openStream(final String inputFileName) + throws FileNotFoundException, IOException { + final FileReader fileReader = new FileReader(inputFileName); + final BufferedReader inputBufferedReader = new BufferedReader(fileReader); + readStream(inputBufferedReader); + } + static void readStream(final BufferedReader inputStream) throws IOException { String line; int lineCounter = 1; diff --git a/Sd1/P/Wc/readFile/src/test/java/de/hdm_stuttgart/mi/sd1/store/TestRead.java b/Sd1/P/Wc/readFile/src/test/java/de/hdm_stuttgart/mi/sd1/store/TestRead.java index ac7088dfaa5ff3d14cde10a40ab7d1c59a9b8b23..5bd14106cde8ff22890baee8cfc7949cfcc62706 100644 --- a/Sd1/P/Wc/readFile/src/test/java/de/hdm_stuttgart/mi/sd1/store/TestRead.java +++ b/Sd1/P/Wc/readFile/src/test/java/de/hdm_stuttgart/mi/sd1/store/TestRead.java @@ -1,13 +1,23 @@ package de.hdm_stuttgart.mi.sd1.store; +import java.io.FileNotFoundException; +import java.io.IOException; + import org.junit.Test; +import de.hdm_stuttgart.mi.sd1.readfile.ReadFile; + @SuppressWarnings("javadoc") public class TestRead { @Test - public void testNoUserInput() { + 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 } } diff --git a/Sd1/swd1.xml b/Sd1/swd1.xml index d51841f94dc561139cffb5c00d17cd9b6c5e2edd..261d74955b2bcb1375749d9603de9f2a84654e07 100644 --- a/Sd1/swd1.xml +++ b/Sd1/swd1.xml @@ -6709,7 +6709,7 @@ public class InputValidator { <answer> <annotation role="make"> - <para role="eclipse">P/Array/medianCmdLin</para> + <para role="eclipse">P/Array/medianCmdLine</para> </annotation> </answer> </qandaentry> @@ -6740,7 +6740,177 @@ public class InputValidator { <section xml:id="sd1ReadCharStreamsExercise"> <title>Exercises</title> - <para/> + <qandaset defaultlabel="qanda" xml:id="sd1QandaLinenumbers"> + <title>Adding line numbers to text files</title> + + <qandadiv> + <qandaentry> + <question> + <para>We want to add line numbers to arbitrary text files not + necessarily being related to programming. Consider the following + HTML example input:</para> + + <programlisting language="java"><html> + <head> + <title>A simple HTML example</title> + </head> + <body> + <p>Some text ... </p> + </body> +</html></programlisting> + + <para>Your application's shall create the following + output:</para> + + <programlisting language="none">1: <html> +2: <head> +3: <title>A simple HTML example</title> +4: </head> +5: <body> +6: <p>Some text ... </p> +7: </body> +8: </html></programlisting> + + <para>Hints:</para> + + <orderedlist> + <listitem> + <para>Given the name of an existing file you may create an + instance of <classname + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html">BufferedReader</classname>:</para> + + <programlisting language="java">final FileReader fileReader = new FileReader(inputFileName); +final BufferedReader inputBufferedReader = new BufferedReader(fileReader); +</programlisting> + </listitem> + + <listitem> + <para>You will have to deal with possible <classname + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/io/FileNotFoundException.html">FileNotFoundException</classname> + problems providing meaningful error messages.</para> + </listitem> + + <listitem> + <para>The <classname + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html">BufferedReader</classname> + class provides a method <methodname + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#readLine--">readLine()</methodname> + allowing to access a given file's content line by + line.</para> + + <caution> + <para>Even if a file exists you have my encounter + <classname + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/io/IOException.html">IOException</classname> + problems being related to <acronym>i.e.</acronym> missing + permissions.</para> + </caution> + </listitem> + </orderedlist> + </question> + + <answer> + <annotation role="make"> + <para role="eclipse">P/Wc/readFile</para> + </annotation> + + <para>This solutions reacts both to inexistent files and general + IO problems:</para> + + <programlisting language="none">File not found: Testdata/input.java</programlisting> + + <para>Two test cases deal both with readable and non-existing + files: and expected exceptions: </para> + + <programlisting language="java"> @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 + } +</programlisting> + + <para>Notice the second test which will only succeed if a + <classname + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/io/FileNotFoundException.html">FileNotFoundException</classname> + is being thrown.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaWc"> + <title>A partial implementation of GNU UNIX + <command>wc</command></title> + + <qandadiv> + <qandaentry> + <question> + <para>In this exercise we will partly implement the (Gnu) UNIX + command line tool <command>wc</command> (word count). Prior to + starting this exercise you may want to:</para> + + <itemizedlist> + <listitem> + <para>Execute <command>wc</command> for sample text files + like e.g. a Java source file of similar:</para> + + <programlisting language="bourne">goik >wc BoundedIntegerStore.java + 58 198 1341 BoundedIntegerStore.java +</programlisting> + + <para>What do these three numbers 58, 198 and 1341 mean? + Execute <command>wc</command> <option>--help</option> or + <command>man</command> <option>wc</option> or read the <link + xlink:href="http://www.gnu.org/software/coreutils/manual/html_node/wc-invocation.html">HTML + documentation</link>.</para> + </listitem> + + <listitem> + <para>wc may process several file in parallel thereby + producing an extra line summing up all values:</para> + + <programlisting language="bourne">goik >wc bibliography.xml swd1.xml + 69 83 2087 bibliography.xml + 6809 18252 248894 swd1.xml + 6878 18335 250981 total +</programlisting> + </listitem> + + <listitem> + <para><command>wc</command> can be used in <link + xlink:href="http://en.wikipedia.org/wiki/Pipeline_(Unix)">pipes</link> + () like:</para> + + <programlisting language="bourne">goik >grep int BoundedIntegerStore.java | wc + 12 76 516</programlisting> + + <para>The above output <quote>12 76 516</quote> tells us + that our file <filename>BoundedIntegerStore.java</filename> + does have 12 lines containing the string + <quote>int</quote>.</para> + </listitem> + </itemizedlist> + + <para>A partial implementation shall offer all features being + mentioned in the introduction. The following steps are a + proposal for your implementation:</para> + + <orderedlist> + <listitem> + <para>Read input either from a list of files </para> + </listitem> + + <listitem> + <para/> + </listitem> + </orderedlist> + </question> + </qandaentry> + </qandadiv> + </qandaset> </section> </chapter>