Java File I/O

Here is a quick example on how to read and write with objects.

Also, if you haven’t been over to Yong Mook Kim’s site,  check it out.  He has plenty of tutorials to take a look at and this one will take to his guides on file I/O in Java.

http://www.mkyong.com/tutorials/java-io-tutorials/

 
Employee object class

import java.io.Serializable;
 
public class Employee implements Serializable{
 
	   private String firstName;
	   private String lastName;
	   private int ID;
	   
	   public Employee(){
		   this.firstName = null;
		   this.lastName = null;
		   this.ID = -1;
	   }
	   
	   public Employee(int ID, String firstName, String lastName){
			if(ID > 0 )
				this.ID = ID;
			else
				this.ID = -1;
			
			this.firstName = firstName;
			this.lastName = lastName;	   
	   }
	   
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public int getID() {
		return ID;
	}
	public void setID(int ID) {
		if(ID > 0 )
			this.ID = ID;
	}
	@Override
	public String toString() {
		return "Employee [firstName=" + firstName + ", lastName=" + lastName
				+ ", ID=" + ID + "]";
	}	   
}

File writer class

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;

public class Output {
	private BufferedWriter writer = null;
	
	public Output(Employee emp, String path){
		try{			
			ObjectOutputStream stream = new ObjectOutputStream( new FileOutputStream( path ) );   
			stream.writeObject(emp);
			stream.close();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}				
}

File reader class

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Input {
	private ObjectInputStream stream;
	
	public Input(String path) throws FileNotFoundException, IOException{
		this.stream = new ObjectInputStream( new FileInputStream(path) );
	}
	
	public Employee get() throws IOException{
		try{
			Employee emp = (Employee) stream.readObject();
			return(emp);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
		finally{
			this.stream.close();
		}		
	}
}

Main function

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;


public class Run {

	public static void main(String[] args) {
		Path path = Paths.get(System.getProperty("user.home"),"foo", "employee.log"); //Note: this won't create the file or directory
		Employee empOut = new Employee(1,"Bit","Diddler");
		Output out = new Output( empOut, path.toString() );
		
		try {
			Input in = new Input(path.toString());
			Employee empIn = in.get();
			System.out.println(empIn.toString());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
}

Sample Output from Main:
Employee [firstName=Bit, lastName=Diddler, ID=1]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s