Exercises
Write a utility method for copying all of an
InputStreamto anOutputStream, without using any temporary files. Provide another solution, without a loop, using operations from theFilesclass, 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
Scanneris 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) aScannerandhasNextLine/nextLine, (b) aBufferedReaderandreadLine, (c) aBufferedReaderandlines. Which is the fastest? The most convenient?When an encoder of a
Charsetwith 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 thenewEncodermethod to get an encoder, and call itsreplacementmethod 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
MessageDigestclass and write a program that computes the SHA-512 digest of a file. Feed blocks of bytes to theMessageDigestobject with theupdatemethod, then display the result of callingdigest. Verify that your program produces the same result as thesha512sumutility.Write a utility method for producing a ZIP file containing all files from a directory and its descendants.
Using the
URLConnectionclass, 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
Authorizationto 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.replaceAlland 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
Pointwith instance variables forxandy. Write a program that serializes an array ofPointobjects to a file, and another that reads the file.Continue the preceding exercise, but change the data representation of
Pointso 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
LocalDateclass is serialized. Why does the class definewriteExternalandreadExternalmethods even though it doesn’t implementExternalizable? (Hint: Look at theSerclass.) Why does the class define areadObjectmethod? How could it be invoked?