0% found this document useful (0 votes)
159 views4 pages

A Guide To Advanced Java: Advj-Lab3

This document provides a guide for a lab on Java IO which focuses on input/output streams, files, and serialization. It outlines objectives for reading and writing to files using streams, listing directory contents, and copying file data. Exercises are provided to practice skills like encrypting file contents, reading and writing student objects to files, and listing directory contents similarly to the 'dir' command.

Uploaded by

Nam Hoang Hoai
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views4 pages

A Guide To Advanced Java: Advj-Lab3

This document provides a guide for a lab on Java IO which focuses on input/output streams, files, and serialization. It outlines objectives for reading and writing to files using streams, listing directory contents, and copying file data. Exercises are provided to practice skills like encrypting file contents, reading and writing student objects to files, and listing directory contents similarly to the 'dir' command.

Uploaded by

Nam Hoang Hoai
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

AdvJ-Lab3- Java IO

A GUIDE TO ADVANCED JAVA


Module 3: java.io package Lab Guide for Lab3

Session Objectives
In this session, you will be practicing with InputStream class and its subclasses OutputStream class and its subclasses File class Buffered Streams Serialization

Part 1 Getting started (30 minutes)


1. Using streams to read data from a file.
The following program read a text file and display data on the screen.Create ReadFile.java file. Scan the code first, type the code, compile and run program.
import import import import import java.io.File; java.io.BufferedReader; java.io.FileReader; java.io.IOException; java.io.FileNotFoundException;

public class ReadFile { public static void main(String[] args) { // Create a file object File file = new File("D:\\test.txt"); try { // Open file to read BufferedReader br = new BufferedReader(new FileReader(file)); String data = ""; while( (data = br.readLine())!=null ) { // Display a string line System.out.println(data); } } br.close();

AdvJ-Lab3- Java IO
catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } } }

Questions: - How to copy data of a file to another file? Hint: using BufferedWriter. 2. Listing the contents of a directory (in a file sytem). Copy the code, compile and run the program.
import java.io.File; public class Dir { static void listPath(File path) { // get all files (or directories) in File path File[] files = path.listFiles(); for (int i = 0, i < files.length; i++) { System.out.println(files[i].toString()); } } public static void main(String args[]) { listPath(new File("C:\\WINDOWS")); } }

Questions: - How to get all files in sub-directories of root Directory? Hint: using recursive method.

Part 2 Workshops (30 minutes)


Quickly look at Module 3s workshops for reviewing basic steps for using byte streams, writting and reading data from a file. Try to compile, run and observe the output of sample code provided for related workshop. Discuss with your class-mate and your instructor if needed.

AdvJ-Lab3- Java IO

Part 3 Lab Assignment (60 minutes)


Do the assignment for Module 3 carefully. Discuss with your class-mates and your instructor if needed. See ACT_Module3_Assignment.pdf file.

Part 4 Do it your self


Exercise 1. Write a program to read a text file and encrypt it by shifting every character forward (in the ASCII table): all characters a replaced by d, b replaced by e, c replaced by f, and so on. Save the encrypted content to an output text file. Exercise 2. Create a class Student includes name, age, mark and necessary methods. Using FileWriter, FileReader and BufferedReader to write a program that has functional menu: Menu ------------------------------------------------1. Add a list of Students and save to File 2. Loading list of Students from a File

3. Exit Your choice: _ + Save to File: input information of several students and write that information into a text file, each student in a line (use tabs to separate the fields) + Read File: read and display information of students Exercise 3. Redo the Exercise 2 by using the classes ObjectInputStream and ObjectOutputStream. Exercise 4. Using BufferredInput/OutputStream to copy data from a file that has capacity exceed 10MB to another file. Exercise 5. Write a program to imitate the dir program. The path of directory is a parameter input from keyboard. List all files and sub directories with details as below figure.

AdvJ-Lab3- Java IO

You might also like