Archive

Posts Tagged ‘Java IO Programs’

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


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


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


Java Program to read data from a file through inputfilestream and show it on the console. – Q34

January 17, 2009 Leave a comment

Q34: Java Program to read data from a file through inputfilestream and show it on the console.

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

class File_read{

	public static void main(String args[]) throws IOException{
		
		String f_name = JOptionPane.showInputDialog("Enter a File name: ");
		
		File f = new File(f_name);
		InputStream file = new FileInputStream(f);
		
		int fsize = file.available();
		
		System.out.println("Reading File : " + f.getName() + "\n");
		
		for(int i=0; i<fsize; i++)
			System.out.print((char)file.read());
	}
}

… from College notes (BCA/MCA assignments):