Exercises
Write a utility method for copying all of an
InputStream
to anOutputStream
, without using any temporary files. Provide another solution, without a loop, using operations from theFiles
class, using a temporary file.Write a program that reads a text file and produces a file with the same name but extension
.toc
, containing an alphabetized list of all words in the input file together with a list of line numbers in which each word occurs. Assume that the file’s encoding is UTF-8.Write a program that reads a file containing text and, assuming that most words are English, guesses whether the encoding is ASCII, ISO 8859-1, UTF-8, or UTF-16, and if the latter, which byte ordering is used.
Using a
Scanner
is convenient, but it is a bit slower than using aBufferedReader
. Read in a long file a line at a time, counting the number of input lines, with (a) aScanner
andhasNextLine
/nextLine
, (b) aBufferedReader
andreadLine
, (c) aBufferedReader
andlines
. Which is the fastest? The most convenient?When an encoder of a
Charset
with partial Unicode coverage can’t encode a character, it replaces it with a default—usually, but not always, the encoding of"?"
. Find all replacements of all available character sets that support encoding. Use thenewEncoder
method to get an encoder, and call itsreplacement
method to get the replacement. For each unique result, report the canonical names of the charsets that use it.The BMP file format for uncompressed image files is well documented and simple. Using random access, write a program that reflects each row of pixels in place, without writing a new file.
Look up the API documentation for the
MessageDigest
class and write a program that computes the SHA-512 digest of a file. Feed blocks of bytes to theMessageDigest
object with theupdate
method, then display the result of callingdigest
. Verify that your program produces the same result as thesha512sum
utility.Write a utility method for producing a ZIP file containing all files from a directory and its descendants.
Using the
URLConnection
class, read data from a password-protected web page with “basic” authentication. Concatenate the user name, a colon, and the password, and compute the Base64 encoding:String input = username + ":" + password; String encoding = Base64.getEncoder().encodeToString(input.getBytes());
Set the HTTP header
Authorization
to the value"Basic " + encoding
. Then read and print the page contents.Using a regular expression, extract all decimal integers (including negative ones) from a string into an
ArrayList<Integer>
(a) usingfind
, and (b) usingsplit
. Note that a+
or-
that is not followed by a digit is a delimiter.Using regular expressions, extract the directory path names (as an array of strings), the file name, and the file extension from an absolute or relative path such as
/home/cay/myfile.txt
.Come up with a realistic use case for using group references in
Matcher.replaceAll
and implement it.Implement a method that can produce a clone of any serializable object by serializing it into a byte array and deserializing it.
Implement a serializable class
Point
with instance variables forx
andy
. Write a program that serializes an array ofPoint
objects to a file, and another that reads the file.Continue the preceding exercise, but change the data representation of
Point
so that it stores the coordinates in an array. What happens when the new version tries to read a file generated by the old version? What happens when you fix up theserialVersionUID
? Suppose your life depended upon making the new version compatible with the old. What could you do?Which classes in the standard Java library implement
Externalizable
? Which of them usewriteReplace
/readResolve
?Unzip the API source and investigate how the
LocalDate
class is serialized. Why does the class definewriteExternal
andreadExternal
methods even though it doesn’t implementExternalizable
? (Hint: Look at theSer
class.) Why does the class define areadObject
method? How could it be invoked?