Archive

Posts Tagged ‘Java IO Programs’

Java Program to handle object serialization using ObjectInputStream and ObjectOutputStream – Q43

January 22, 2009 Leave a comment

Q43: Java Program to handle object serialization using ObjectInputStream and ObjectOutputStream.

import java.io.*;

public class SerializationDemo{
	public static void main(String args[]){
		try{
			Myclass object1 = new Myclass("Hello", -7, 2.7e10);
			System.out.println("object1: " + object1);
			FileOutputStream fos = new FileOutputStream("serial");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(object1);
			oos.flush();
			oos.close();
		}
		catch(IOException e){
			System.out.println("Exception during serialization: " + e);
			System.exit(0);
			
		}
		try{
			Myclass object2;
			FileInputStream fis = new FileInputStream("serial");
			ObjectInputStream ois = new ObjectInputStream(fis);
			object2 = (Myclass)ois.readObject();
			ois.close();
			System.out.println("object2: " + object2);
		}
		catch(Exception e){
			System.out.println("Exception during Deserialization: " + e);
			System.exit(0);
		}
	}
}
class Myclass implements Serializable{
	String s;
	int i;
	double d;
	public Myclass(String s, int i, double d){
		this.s = s;
		this.i = i;
		this.d = d;
	}
	public String toString(){
		return "s= " + s + "; i= " + i + "; d= " + d;
	}
}

… from College notes (BCA/MCA assignments):


Advertisement

Java Program to print integers you have input through console using BufferedReader and StringTokenizer – Q42

January 21, 2009 Leave a comment

Q42: Java Program to print integers you have input through console using BufferedReader and StringTokenizer.

import java.util.*;
import java.io.*;

class Buf_R_Str_Token{
	public static void main(String args[]) throws IOException{
		BufferedReader b_r = new BufferedReader(new 
InputStreamReader(System.in));
		String str = b_r.readLine();
		
		StringTokenizer st = new StringTokenizer(str, ",");
		
		String item;
		try{
			while((item = st.nextToken()) != null){
				System.out.print(" " + item);
			}
		}
		catch(Exception e){
			System.out.println("\n End of String.");
		}
	}
}

… from College notes (BCA/MCA assignments):


Java Program to calculate number of characters, words and lines while reading from a input stream (file or console) – Q41

January 21, 2009 Leave a comment

Q41: Java Program to calculate number of characters, words and lines while reading from a input stream (file or console)

import java.io.*;

class WordCount{
	public static int words = 0;
	public static int lines = 0;
	public static int chars = 0;
	public static void wc(InputStreamReader isr) throws IOException{
		int c = 0;
		boolean lastWhite = true;
		String WhiteSpace = " \t\n\r";
		while((c = isr.read()) != -1){
			chars++;
			if(c == '\n')
				lines++;
			int index = WhiteSpace.indexOf(c);
			if(index == -1){
				if(lastWhite == true){
					++words;
				}
			lastWhite = false;
			}
			else
				lastWhite = true;
		}
		if(chars != 0)
			++lines;
	}
	public static void main(String args[]){
		FileReader fr;
		try{
			if(args.length == 0) {
				wc(new InputStreamReader(System.in));
			}
			
			else{
				for(int i=0; i<args.length; i++){
					fr = new FileReader(args[i]);
					wc(fr);
				}
			}
		}
		catch(IOException e){
			return;
		}
		System.out.println(lines + " " + words + " " + chars);
	}
}

… from College notes (BCA/MCA assignments):


Java Program by using PushBackReader to utilize unread method – Q40

January 20, 2009 Leave a comment

Q40: Java Program by using PushBackReader to utilize unread method.

import java.io.*;

class PB_Inp_Stream{
	public static void main(String args[]) throws IOException{
		String s = " if (a== 4) a = 0;\n";
		byte buf[] = s.getBytes();
		ByteArrayInputStream in = ByteArrayInputStream(buf);
		PushbackInputStream f = PushbackInputStream(in);
		int c;
		
		while((c = f.read()) != -1){
			switch(c){
				case '=':
					if((c = f.read()) != -1){
						System.out.print(".eq.");
					}
					else{
						System.out.print("<-");
						f.unread();
					}
					break;
				default:
					System.out.print((char) c);
					break;
			}
		}
	}
}

… from College notes (BCA/MCA assignments):


Java Program by using BufferedReader stream to use mark and reset methods – Q39

January 20, 2009 Leave a comment

Q39: Java Program by using BufferedReader stream to use mark and reset methods.

import java.io.*;
class Buf_Inp_Stream{
	public static void main(String args[]) throws IOException{
		String str = "This is s &copy; copyright symbol " +
					 "but this is &copy not.";
		byte buf[] = str.getBytes();
		ByteArrayInputStream in = ByteArrayInputStream(buf);
		BufferedInputStream f = new BufferedInputStream(in);
		int c;
		boolean marked = false;
		while((c = f.read()) != -1){
			switch(c){
				case '&':
					if(!marked){
						f.mark(32);
						marked = true;
					}
					else{
						marked = false;
					}
					break;
				case ';':
					if(marked){
						marked = false;
						System.out.print("(c)");
					}
					else{
						System.out.print((char) c);
					}
					break;
				case ' ':
					if(marked){
						marked = false;
						f.reset();
						System.out.print("&");
					}
					else{
						System.out.print((char) c);
					}
					break;
				default:
					if(!marked)
						System.out.print((char) c);
					break;
			}
		}
	}
}

… from College notes (BCA/MCA assignments):