You are on page 1of 11

Rajagiri School of Engineering & Technology                                         Uday Babu P.

I/O Sample Programs


1. Write a Java program to add two double values and hence to find its square root and cube.
Program
import java.io.*;
class AddSqPow {
public static void main(String args[])
{
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double a, b, c, p, sr;
System.out.println("Enter two numbers");
a = Double.parseDouble(br.readLine());
b = Double.parseDouble(br.readLine());
c = a + b;
System.out.println("Sum : "+ c);
sr = Math.sqrt(c);
System.out.println("Square root of " + c + " = " + sr);
p = Math.pow(c,3);
System.out.println("Cube of " + c + " = " + p);
}
catch(IOException e)
{
System.out.println("Exception : "+e);
}

CS206    Object Oriented Design and Programming         KTU                           1
Rajagiri School of Engineering & Technology                                         Uday Babu P.

}
}
Output
Enter two numbers
5.5
3.5
Sum : 9.0
Square root of 9.0 = 3.0
Cube of 9.0 = 729.0

2. Write a Java program to read the grade of a student from console and to print a suitable greeting.
Program
import java.io.*;
class ReadGrade {
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char g;
System.out.println("Enter the grade secured in STLD");
g = (char)br.read();
if(g=='A')
{
System.out.println("Excellent");
}
else if(g=='B')
{
System.out.println("Very Good");

CS206    Object Oriented Design and Programming         KTU                           2
Rajagiri School of Engineering & Technology                                         Uday Babu P.

}
else
{
System.out.println("Improvement Required");
}
}
}
Output
Enter the grade secured in STLD
A
Excellent

3. Write a Java program to read the name of a student and to write it into a file.
Program
import java.io.*;
class ReadString {
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name;
System.out.println("Enter your name");
name = br.readLine();
System.out.println("Your name is "+ name);
FileWriter fout = new FileWriter("test.txt");
fout.write("Name of student :" );
fout.write(name);
fout.close( );

CS206    Object Oriented Design and Programming         KTU                           3
Rajagiri School of Engineering & Technology                                         Uday Babu P.

}
}
Output
Enter your name
Uday Babu P
Your name is Uday Babu P

4. Write a Java program to find the sum of the integer values in a file test.txt
Program
import java.util.*;
import java.io.*;
class SumFile {
public static void main(String args[]) throws IOException {
int sum = 0, num;
FileReader fin = new FileReader("test.txt"); //opening file
Scanner fscan = new Scanner(fin);
while(fscan.hasNextInt()==true) {
num = fscan.nextInt();
sum += num; //sum=sum+num;
}
fscan.close();
fin.close();
System.out.println("Sum = " + sum);
}
}
Output
Sum = 17

CS206    Object Oriented Design and Programming         KTU                           4
Rajagiri School of Engineering & Technology                                         Uday Babu P.

5. Write a Java program to display the contents of a file.


Program
import java.io.*;
import java.util.*;
class ShowFile {
public static void main(String args[]) throws IOException
{
int i; char c;
String fname;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the filename");
fname=scan.next();
FileInputStream fin = new FileInputStream(fname); //opening file
while((i = fin.read())!=-1) {
c=(char)i;
System.out.print(c);
}
System.out.println("Reading Completed");
fin.close();
}
}
Output
Enter the filename
test.txt
1 3 6 7 numbers
Reading Completed

CS206    Object Oriented Design and Programming         KTU                           5
Rajagiri School of Engineering & Technology                                         Uday Babu P.

6. Write a Java program to copy the contents of the file input.txt into another file.
Program
import java.io.*;
import java.util.*;
class CopyFile {
public static void main(String args[])
{
int i;
String f2;
Scanner scan=new Scanner(System.in);
FileInputStream fin;
FileOutputStream fout;
System.out.println("Enter Output filename");
f2=scan.next();
try {
fin = new FileInputStream("input.txt"); //opening file
fout = new FileOutputStream(f2); //opening file
}
catch(FileNotFoundException e) {
System.out.println("Cannot Open File !! File not found");
return;
}
try {
while((i = fin.read())!=-1) {
fout.write(i);
}
System.out.println("Copying Completed");

CS206    Object Oriented Design and Programming         KTU                           6
Rajagiri School of Engineering & Technology                                         Uday Babu P.

}
catch(IOException e) {
System.out.println("Error Reading or writing File");
}
finally{
try {
fin.close();
fout.close();
} catch(IOException e) {
System.out.println("Error Closing File");
}
}

}
}

Output
Enter Output filename
data.txt
Copying Completed

Note: Use throws clause to remove try catch blocks. The FileNotFoundException is subclass of
IOException, so throws clause needs to list only IOException.

CS206    Object Oriented Design and Programming         KTU                           7
Rajagiri School of Engineering & Technology                                         Uday Babu P.

7. Write a Java program to find the number of words in a file.


Method I
Program
import java.io.*;
class CountWords {
public static void main(String args[]) throws IOException
{
int i, count=0;
char c;
FileInputStream fin = new FileInputStream("Input.txt");
while((i = fin.read())!=-1)
{
c= (char) i;
if(c==' ')
{
count++;
}
}
fin.close();
count++;
System.out.println("No: of words="+count);
}
}
Output
No: of words=5

CS206    Object Oriented Design and Programming         KTU                           8
Rajagiri School of Engineering & Technology                                         Uday Babu P.

Method II
Program
import java.util.*;
import java.io.*;
class WordCount {
public static void main(String args[]) throws IOException {
int count=0;
FileReader fin = new FileReader("input.txt"); //opening file
Scanner fscan = new Scanner(fin);
while(fscan.hasNext()==true) {
fscan.next();
count++;
}
System.out.println("Count = "+count);
fscan.close();
fin.close();
}
}
Output
Count = 5

Method III
Program
import java.io.*;
class CountWords {
public static void main(String args[]) throws IOException

CS206    Object Oriented Design and Programming         KTU                           9
Rajagiri School of Engineering & Technology                                         Uday Babu P.

{
int i, count=0;
char c;
FileReader fin = new FileReader("Input.txt");
while((i = fin.read())!=-1)
{
c= (char) i;
if(c==' ')
{
count++;
}
}
fin.close();
count++;
System.out.println("No: of words="+count);
}
}
Output
No: of words=5

8. Write a Java program to read a file and to separate odd and even numbers in it to separate files.
Program
import java.util.*;
import java.io.*;
class OddEven {
public static void main(String args[]) throws IOException {

CS206    Object Oriented Design and Programming         KTU                           10
Rajagiri School of Engineering & Technology                                         Uday Babu P.

int num;
FileReader fin = new FileReader("test.txt");
FileWriter fout1 = new FileWriter("odd.txt");
FileWriter fout2 = new FileWriter("even.txt");
Scanner fscan = new Scanner(fin);
while(fscan.hasNextInt()==true) {
num = fscan.nextInt();
if(num%2==1)
{
fout1.write(Integer.toString(num)+ "\t");
}
else
{
fout2.write(Integer.toString(num)+ "\t");
}
}
fscan.close();
fin.close();
fout1.close();
fout2.close();
System.out.println("Success");
}
}
Output
Success

CS206    Object Oriented Design and Programming         KTU                           11

You might also like