DataFileTest.java Source code

Java tutorial

Introduction

Here is the source code for DataFileTest.java

Source

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class DataFileTest {
    static void writeData(Employee e, PrintWriter out) throws IOException {
        e.writeData(out);
    }

    static Employee readData(BufferedReader in) throws IOException {
        Employee e = new Employee();
        e.readData(in);
        return e;
    }

    public static void main(String[] args) {
        Employee staff = new Employee("Java Source", 35500);

        staff.raiseSalary(5.25);

        try {
            PrintWriter out = new PrintWriter(new FileWriter("employee.dat"));
            writeData(staff, out);
            out.close();
        } catch (IOException e) {
            System.out.print("Error: " + e);
            System.exit(1);
        }

        try {
            BufferedReader in = new BufferedReader(new FileReader("employee.dat"));
            Employee e = readData(in);
            e.print();
            in.close();
        } catch (IOException e) {
            System.out.print("Error: " + e);
            System.exit(1);
        }
    }
}

class Employee {

    private String name;

    private double salary;

    public Employee(String n, double s) {
        name = n;
        salary = s;
    }

    public Employee() {
    }

    public void print() {
        System.out.println(name + " " + salary);
    }

    public void raiseSalary(double byPercent) {
        salary *= 1 + byPercent / 100;
    }

    public void writeData(PrintWriter out) throws IOException {
        out.println(name + "|" + salary);
    }

    public void readData(BufferedReader in) throws IOException {
        String s = in.readLine();
        StringTokenizer t = new StringTokenizer(s, "|");

        name = t.nextToken();

        salary = Double.parseDouble(t.nextToken());
    }

}