DataOutputStream & DataInputStream is use to store primitive data type value in file in java.This program take integer value to user and store txt file and display all number and also display even odd number in file.
CODING:
import java.io.*;
import java.util.Scanner;
public class data {
public static void main(String[] args)throws IOException{
int ch=0,odd=0,even=0;
Scanner sc=new Scanner(System.in);
File f=new File("EvenOdd.txt");
FileOutputStream out=new FileOutputStream(f);
DataOutputStream o=new DataOutputStream(out);
for(int i=0;i<5;i++){
System.out.println("Enter Number");
ch=sc.nextInt();
if(ch%2==0){
even+=1;
}else{
odd+=1;
}
o.writeInt(ch);
}
o.close();
FileInputStream in=new FileInputStream(f);
DataInputStream di=new DataInputStream(in);
ch=0;
for(int i=0;i<5;i++){
ch=di.readInt();
System.out.println(ch);
}
di.close();
System.out.println("Odd Number "+odd+"\nEven number "+even);
}
}
import java.util.Scanner;
public class data {
public static void main(String[] args)throws IOException{
int ch=0,odd=0,even=0;
Scanner sc=new Scanner(System.in);
File f=new File("EvenOdd.txt");
FileOutputStream out=new FileOutputStream(f);
DataOutputStream o=new DataOutputStream(out);
for(int i=0;i<5;i++){
System.out.println("Enter Number");
ch=sc.nextInt();
if(ch%2==0){
even+=1;
}else{
odd+=1;
}
o.writeInt(ch);
}
o.close();
FileInputStream in=new FileInputStream(f);
DataInputStream di=new DataInputStream(in);
ch=0;
for(int i=0;i<5;i++){
ch=di.readInt();
System.out.println(ch);
}
di.close();
System.out.println("Odd Number "+odd+"\nEven number "+even);
}
}
Comments
Post a Comment