View RSS Feed

fatabass

Writing an Array Object to a Binary File and Reading Back

Rating: 1 votes, 1.00 average.
by , 01-13-2012 at 11:03 PM (4327 Views)
Java Code:
package myPackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Scanner;

public class TestClass implements Serializable {

	public static void main(String[] args) {
		
		int[] myIntegerArray = new int[3];
		
		myIntegerArray[0] = 1;
		myIntegerArray[1] = 2;
		myIntegerArray[2] = 3;
		
		for(int i=0; i<myIntegerArray.length; i++)
		{
			System.out.println(myIntegerArray[i]);
		}
		
		try 
		{
			ObjectOutputStream myStream = new ObjectOutputStream(new FileOutputStream("c:/datt.dat"));
			myStream.writeObject(myIntegerArray);
			myStream.close();
		} 
		catch (FileNotFoundException e) 
		{
		} 
		catch (IOException e) 
		{
		}
		
		try 
		{
			ObjectInputStream mySecondStream = new ObjectInputStream(new FileInputStream("c:/datt.dat"));
			int[] koray = (int[]) mySecondStream.readObject();
			for(int i=0; i<koray.length; i++)
			{
				System.out.println(koray[i]);
			}
		} 
		catch (FileNotFoundException e) 
		{
		} catch (IOException e) 
		{
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Submit "Writing an Array Object to a Binary File and Reading Back" to Facebook Submit "Writing an Array Object to a Binary File and Reading Back" to Digg Submit "Writing an Array Object to a Binary File and Reading Back" to del.icio.us Submit "Writing an Array Object to a Binary File and Reading Back" to StumbleUpon Submit "Writing an Array Object to a Binary File and Reading Back" to Google

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments