Java Assignment- 6
I/O streams & Java 8 Files API
Basic Questions
- Write a short program that creates a File object for “notes.txt”, prints its absolute path, prints whether it exists, and prints whether it is a file or a directory.
- Write a program that creates an empty text file named “hello.txt” if it does not exist, and prints “created” or “already exists”.
- Write a program that deletes the file “temp.txt” if it exists and prints a clear message about the result.
- Write a program that writes three lines of text to “greetings.txt” using FileWriter, then closes the writer.
- Write a program that reads and prints all lines from “greetings.txt” using FileReader and a simple loop over read() or a small char buffer.
- Write a program that writes the line “Buffered write demo” to “buffered.txt” using BufferedWriter, then reads it back using BufferedReader and prints the line.
- Write a program that copies text from “input.txt” to “output.txt” using BufferedReader and BufferedWriter (line by line).
- Write a program that reads one full line of console input using Scanner, then writes that line into “console.txt” and prints “saved”.
- Write a program that writes the bytes {65, 66, 67} to “abc.bin” using FileOutputStream, then closes the stream.
- Write a program that reads all bytes from “abc.bin” using FileInputStream and prints each byte value on one line.
- Write a program that uses InputStreamReader with “UTF-8” to read text from “utf8.txt” and prints it to the console.
- Write a program that writes the text “नमस्ते” to “unicode.txt” using OutputStreamWriter with “UTF-8” encoding.
- Write a program that prints the size (in bytes) of “greetings.txt” using File.length().
- Write a program that lists the names of all files and folders in the current directory using File.listFiles() and prints each name.
- Write a program that appends the line “Appended line” to “append.txt” using FileWriter in append mode.
- Write a program that counts and prints the number of lines in “lines.txt” using BufferedReader.
- Write a program that replaces every space ‘ ‘ with underscore ‘_’ while copying from “source.txt” to “underscored.txt” using character streams.
- Write a program that explains (in comments) byte streams vs character streams, then prints a message choosing which you would use to copy an image file and why.
- Write a program that reads integers from the console using Scanner until the user types “stop”, then writes the numbers to “numbers.txt” separated by spaces.
- Write a program that checks if “logs” directory exists; if not, create it, then create an empty file “logs/app.log” and print a success message.
Intermediate Questions
- Write a program that copies a binary file “photo.jpg” to “photo_copy.jpg” using FileInputStream and FileOutputStream with a byte buffer (e.g., byte[4096]).
- Write a program that compares the time it takes to copy “big.txt” using FileReader/FileWriter character-by-character versus using BufferedReader/BufferedWriter line-by-line, and prints both times in milliseconds.
- Write a program that reads “data.txt” with BufferedReader and prints the total number of characters, words, and lines (simple word split by whitespace).
- Write a program that converts the encoding of a text file from “UTF-8” to “ISO-8859-1” by reading with InputStreamReader(“UTF-8”) and writing with OutputStreamWriter(“ISO-8859-1”).
- Write a program that reads integers from “nums.txt” and writes only the even numbers to “evens.txt” (keep the same order).
- Write a program that merges two text files “a.txt” and “b.txt” into “merged.txt” by copying all lines of a.txt then all lines of b.txt.
- Write a program that reads the first N lines of “biglog.txt” (value of N taken from console) and writes them to “head.txt”.
- Write a program that reads “paragraphs.txt” and writes only non-empty lines to “cleaned.txt”.
- Write a program that lists only files (not directories) in a folder “docs” using File.listFiles() and prints their names and sizes.
- Write a program that uses RandomAccessFile on “rand.txt” to write the ASCII letters A to Z at the start of the file, then seeks to position 10 and overwrites one letter with ‘*’.
- Write a program that uses RandomAccessFile to read the 5th byte (index 4) from “bytes.bin” and print its numeric value.
- Write a program that stores three simple records into “records.bin” using RandomAccessFile: each record contains an int id followed by a fixed-length 10-char name (pad with spaces). Then read and print all records.
- Write a program that serializes a simple Student object (fields: id, name) to “student.ser” using ObjectOutputStream.
- Write a program that deserializes the Student object from “student.ser” using ObjectInputStream and prints the fields.
- Write a program that serializes two different objects in sequence to “data.ser” and then deserializes them back in the same order, printing both.
- Write a program that uses the Java 8 Files API to read all lines from “notes.txt” into a List and then prints each line. (Use Files.readAllLines(Path) and a simple loop.)
- Write a program that uses the Java 8 Files API to write a list of strings to “out.txt” (e.g., Files.write(Path, lines, StandardCharsets.UTF_8, CREATE, TRUNCATE_EXISTING)).
- Write a program that uses the Java 8 Files API to copy “from.bin” to “to.bin” and then move “to.bin” into a “backup” directory (create the directory if needed).
- Write a program that uses the Java 8 Files API to print whether “config.yaml” exists, and if it does, prints its size and last modified time.
- Write a program that reads a file path from console and prints “Readable/Writable/Executable” flags using Files.isReadable, Files.isWritable, and Files.isExecutable.
Advanced Questions
- Write a program that uses RandomAccessFile to create a simple index: write 100 integers (1…100) to “ints.bin”, then read the k-th integer by seeking to the correct byte position (assuming 4 bytes per int). Print the value for k entered from the console.
- Write a small log rotator: if “app.log” exceeds 1 MB (check with Files.size), move it to “app.log.1” (overwriting if needed) and create a fresh empty “app.log”.
- Write a program that inserts text into the middle of a file “story.txt” using RandomAccessFile: read the tail into memory, seek to the insert position, write the new text, then write back the tail.
- Write a program that searches for a given byte pattern in “data.bin” using FileInputStream and a small sliding window buffer, and prints the first position where the pattern appears (or “not found”).
- Write a program that serializes an array of Student objects to “students.ser” and then deserializes it back, printing all students. Ensure the class implements Serializable.
- Write a program that demonstrates custom serialVersionUID in a serializable class, serializes an object, then explains in comments what happens if the UID changes later.
- Write a program that uses ObjectOutputStream with writeObject for a class that contains a field marked transient. Serialize and deserialize, and show that the transient field is not restored.
- Write a program that lists all files recursively starting from a given folder using the File API (listFiles() and recursion), and prints their relative paths.
- Write a program that reads a large file using BufferedInputStream and prints the time taken, then reads the same file using plain FileInputStream without buffering and prints the time taken, so you can compare performance.
- Write a program that uses Java 8 Files.newBufferedReader and Files.newBufferedWriter to transform a file line-by-line: for each input line, write the line number and a colon before the line text to the output file.