Lab 4: Java NIO and Object Serialization

In this lab, you will learn to use the modern Java NIO (New Input/Output) API for file operations. You’ll implement utility methods for common file operations using the java.nio.file package.

Prerequisites

  • Java Development Kit (JDK) 11 or later
  • Your preferred Java IDE
  • Git for version control

Getting Started

If your instructor is using GitHub classroom, you will need to accept the assignment using the link below, clone the template repository, and import it as a project into your IDE.

If your instructor is not using GitHub classroom, clone and import the template project at https://github.com/cpit305-spring-25-IT1/lab-04 ↗.

Tasks

Task 1: File Operations Utility

Implement the following static utility methods for file operations in src/main/java/cpit305/fcit/kau/edu/sa/FileUtils.java:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class FileUtils {
    /**
     * Creates a new file and returns success/failure message.
     */
    public static String createFile(String fileName) throws IOException {

        
    }
    
    /**
     * Creates a new directory and returns success/failure message
     */
    public static String createDirectory(String dirName) throws IOException {

      
    }
    
    /**
     * Creates nested directories and returns success/failure message
     */
    public static String createDirectories(String path) throws IOException {


    }
    
    /**
     * Copies a file to target location and returns success/failure message
     */
    public static String copyFile(String source, String target) throws IOException {

    }
    
    /**
     * Moves a file to target location and returns success/failure message
     */
    public static String moveFile(String source, String target) throws IOException {


    }
    
    /**
     * Deletes a file if it exists and returns success/failure message
     */
    public static String deleteIfExists(String pathStr) throws IOException {


    }
}

Task 2: Word Counter

Implement a method that downloads a file by it’s URL and count the number of words in it. Complete the implementation at src/main/java/cpit305/fcit/kau/edu/sa/WordCounter.java :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class WordCounter {

    public static long countWordsByFileUrl(String downloadUrl) throws IOException {
        // Download file
        Path tempFile = Files.createTempFile("tmpFile", ".txt");
        URL url = new URL(downloadUrl);
        Files.copy(url.openStream(), tempFile, StandardCopyOption.REPLACE_EXISTING);

        // Count the number of words
        long wordCount = 0;
        try {





            return wordCount;
        } finally {


        }
    }
}

Task 3: Object Serialization

Implement a student record serializer in src/main/java/cpit305/fcit/kau/edu/sa/StudentSerializer.java for the Student class. Note that the Student class implements the Serializable interface. Use ObjectOutputStream with FileOutputStream to serialize the object and store it in a file. Then , use ObjectInputStream with FileInputStream to deserialize the content of the raw bytes into the Student object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private int id;
    private double gpa;

    public Student(String name, int id, double gpa) {
        this.name = name;
        this.id = id;
        this.gpa = gpa;
    }

    // Getters and setters
    public String getName() { return name; }
    public int getId() { return id; }
    public double getGpa() { return gpa; }
}

1
2
3
4
5
6
7
8
9
public class StudentSerializer {
    public static void serialize(Student student, String fileName) {

    }

    public static Student deserialize(String fileName){
        return null;
    }
}

Testing Your Implementation

Run the included unit tests before you push to GitHub.

Deliverables and Submission

Please push your code to GitHub for auto-grading and submit a PDF file with screenshots of your work.