Object Oriented Programming

Files

Files

Table of Contents

Input: Command line arguments

void main(String[] args)

Write a program that creates a Person object from 3 command line arguments (age, height, name), and then outputs the object as a string

class Program {
    static void main(String[] args) {
        int age = Integer.parseInt(args[0]);
        double height = Double.parseDouble(args[1]);
        String name = args[2];
        Person person = new Person(age, height, name);
        System.out.println(person);
    }
}

Input: Scanner

Read in various data types

Scanner reads in a single value matching the method name

boolean b = scanner.nextBoolean();
int i = scanner.nextInt();
double d = scanner.nextDouble();

Scanner example

Write a program that accepts three user inputs, creates an IMDB entry for an Actor and prints the object:

import java.util.Scanner;

public class Program {
    public static void main(String[] args) {
        String name = scanner.nextLine();
        double rating = scanner.nextDouble();
        scanner.nextLine();
        String review = scanner.nextLine();
        Actor actor = new Actor(name, rating, review);
        System.out.println(actor);
    }
}

public class Actor {
    public static final int MAX_RATING = 10;
    public String name;
    public double rating;
    public String review;
    
    public Actor(String name, double rating, String review) {
        this.name = name;
        this.rating = rating;
        this.review = review;
    }
    
    public String toString() {
        return String.format("You gave %s a rating of %f%d\n",
            name, rating, MAX_RATING) +
            String.format("Your review: '%s'", review);
    }
}

Boilerplate: reading plaintext with Scanner

import java.util.scanner;
try (Scanner scanner = new Scanner(new FileReader("test.txt"))) {
    while (scanner.hasNextLine()) {
        // do stuff
    }
}

Reading files

Boilerplate for reading plaintext files

import java.io.FileReader;      // low level file for simple character reading
import java.io.BufferedReader;  // higher level file object that reads Strings
import java.io.IOException;     // handle exceptions

public class ReadFile {
    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
            String text;
            while ((text = br.readLine()) != null) {
                // do stuff with text
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Reading CSV files

String[] columns = text.split(",");

Writing files

Boilerplate for writing plaintext files

import java.io.FileWriter;  
import java.io.PrintWriter;  
import java.io.IOException;

public class Program {
    public static void main(String[] args) {
        try (PrintWriter pw = new FileWriter("test.txt")) {
            pw.println("Hello World");
            pw.format("Test a %s and an integer %d", "string", 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Edit this page.