Joomla Vorlagen by Website Hosting

PJ1 - 9th laboratory (Streams)

This tutorial is based on an Oracle tutorial - https://docs.oracle.com/javase/tutorial/essential/io/streams.html - so you can visit the course website for more insight or self-study.

1. Create a program that copies the specified file using InputStream (FileInputStream) and OutputStream (FileOutputStream). Copying takes place by bytes. Use the faust-goethe-orig.txt file to test it.

2. Modify the previous example to copy using a buffer (use 1MB).

3. Program the previous example using Writer and Reader. Copying is by character and is converted to uppercase.

4. Use the Deflate method for the previous task (the output will be compressed by gzip - GzipOutputStream is used). Properly name the output file - it will contain the extension gz. Examine the file and try decompressing it.

5. Program line-by-line reading (use BufferedReader) to output numbered lines.

6. Use the Scanner class to read words from a file and print them on the console.

7. Use the Scanner class to retrieve double numbers from doubles.txt and calculate their arithmetic mean.

8. Use the DataOutputStream class to write triplets <double, int, String> to the file based on the data:

static final double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };

static final int[] units = { 12, 8, 13, 29, 50 };

static final String[] descs = {

  "Java T-shirt",

  "Java Mug",

  "Duke Juggling Dolls",

  "Java Pin",

  "Java Key Chain"

};

Examine the resulting file.

Use DataInputStream to retrieve and print data.

Homework

Load data from Faust-Goethe-orig.zip. To decompress the data, use ZipInputStream (you must call the getNextEntry method on the ZipInputStream object itself before reading - otherwise the work is the same as with GZIPInputStream); Create an output file (Faust-Goethe-1250.txt.gz) containing text in the format:

<line-number>; <line-length>; <retrieved line from original file>

The file will be in gzip format using the GZIPOutputStream class and the text will be encoded with the windows-1252 character set (you get the character set as “Charset.forName (" windows-1252 ")”). Use the PrintWriter.printf method to write text data.