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

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

January 20, 2009 Leave a comment Go to comments

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 © 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):


Advertisement
  1. No comments yet.
  1. No trackbacks yet.

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: