Archive

Archive for the ‘Java’ Category

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


Java Program to read from a file using FileInputStream and use available(), skip() to go through bytes – Q38

January 19, 2009 Leave a comment

Q38: Java Program to read from a file using FileInputStream and use available(),skip() to go through bytes. Read ¼ n bytes on console, next ¼ n bytes on a byte array , and next ¼ n bytes skip and next ¼ n bytes again print it on console.

import java.io.*;

class FILE_AV_SK{
	public static void main(String args[]) throws IOException{
		int size;
		InputStream f=new FileInputStream("tax.java");
		
		size = f.available();
		System.out.println("Total Available Bytes:" + size);
		
		int n=size/4;
		System.out.println("(1). First "+ n + " bytes of the file Displaying to Console");
		for(int i=0;i<n;i++)
			System.out.print((char) f.read());
			
		System.out.println("\nStill Available: "+ f.available());
		System.out.println("(2). Reading the next " + n +" and Storing to Byte Array.");
		
		byte b[] = new byte[n];
		for(int i=0; i<n; i++)
			b[i] = (byte) f.read();
		
		System.out.println("\nStill Available: "+(size = f.available()));
		System.out.println("(3). Skipping half of remaining bytes with skip()");
		f.skip(n);
		
		System.out.println("Still Available: "+f.available());
		System.out.println("(4). Last "+ n + " bytes of the file Displaying to Console");
		for(int i=0;i<n;i++)
			System.out.print((char) f.read());
		
		f.close();
	}
}

… from College notes (BCA/MCA assignments):


Java Program to write the contents onto a ByteArrayOutputStream and from there write the contents on a file, and on another byte array – Q37

January 19, 2009 Leave a comment

Q37: Java Program to write the contents onto a ByteArrayOutputStream and from there write the contents on a file, and on another byte array.

import java.io.*;
import javax.swing.*;

class B_a_o_s{

	public static void main(String args[]) throws IOException{
		String s = JOptionPane.showInputDialog("Enter a Sentence: 
");
		ByteArrayOutputStream f = new ByteArrayOutputStream();
		byte buff[] = s.getBytes();
		
		f.write(buff);
		System.out.println("Buffer as a String");
		System.out.println(f.toString());
		System.out.println("Into Array");
		byte b[] = f.toByteArray();
		for(int i=0; i<b.length; i++)
			System.out.print((char) b[i]);
		System.out.println("\n To an output Stream");
		
		OutputStream f2 = new FileOutputStream("test.txt");
		f.writeTo(f2);
		f2.close();
		f.reset();
		for(int i=0; i<3; i++)
			f.write('X');
		System.out.println(f.toString());
	}
}

… from College notes (BCA/MCA assignments):