Advertisements

Archive

Archive for January 18, 2009

Java Program to read from byte array using ByteArrayInputStream and print the characters onto console – Q36

January 18, 2009 Leave a comment

Q36: Java Program to read from byte array using ByteArrayInputStream and print the characters onto console.

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

class B_a_i_s{

	public static void main(String args[]){
		String inp = JOptionPane.showInputDialog("Enter a String:");
		
		byte b_arr[] = inp.getBytes();
		
		ByteArrayInputStream in = new ByteArrayInputStream(b_arr);
		
		int c;
		while((c = in.read()) != -1){
			System.out.print((char)c);

		}
	}
}

… from College notes (BCA/MCA assignments):


Advertisements

Java Program to read data from file through inputfilestream and write its contents on another file through fileoutputstream – Q35

January 18, 2009 Leave a comment

Q35: Java Program to read data from file through inputfilestream and write its contents on another file through fileoutputstream.

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

class File_RW{

	public static void main(String args[]) throws IOException{
		
		String f_name = JOptionPane.showInputDialog("Enter a file name: ");
		InputStream infile = new FileInputStream(f_name);
		int fsize = infile.available();
		
		f_name = JOptionPane.showInputDialog("Enter a file name to Save As: ");
		OutputStream outfile = new FileOutputStream(f_name, true);
		
		System.out.println("Reading File : " + f_name + "\n");
		char ch;
		for(int i=0; i<fsize; i++){
			ch = (char)infile.read();
			outfile.write(ch);
			System.out.print(ch);
		}
		infile.close();
		outfile.close();
		System.out.println("File Successfully Copied.");
	}
}

… from College notes (BCA/MCA assignments):