LAB#1
Filing & Calendar in java
LAB OBJECTIVE
1) Write a program that takes 3 numbers as input in a file and prints the contens of the file.
Input:
package l2t1;
import java.io.*;
import java.util.Scanner;
public class L2t1 {
public static void main(String[] args) {
File f;
Scanner sc = new Scanner(System.in);
try{
byte m[] = new byte[3];
System.out.println(“Enter 3 number : “);
for(int i = 0; i < m.length ; i++){
m[i]= sc.nextByte();
}
f = new File(“C:/sss.txt”);
OutputStream os = new FileOutputStream(f);
for(int i =0;i<m.length;i++){
os.write(m[i]);
}
os.close();
InputStream is = new FileInputStream(f);
int size = is.available();
for(int i= 0 ;i<size;i++){
System.out.println(is.read()+” “);
}
}catch(Exception e){ System.out.println(“file not exist”);
}}}
Output:
Enter 3 number :
3
6
5
3
6
5
2) Write a program that converts these numbers into character and print them.
Input:
package l2t2;
import java.io.*;
import java.util.Scanner;
public class L2t2 {
public static void main(String[] args) {
File f;
Scanner sc = new Scanner(System.in);
try{
byte m[] = new byte[3];
System.out.println(“Enter 3 number : “);
for(int i = 0; i < m.length ; i++){
m[i]= sc.nextByte();
}
f=new File(“C:/sss.txt”);
OutputStream os = new FileOutputStream(f);
for(int i =0;i<m.length;i++){
os.write(m[i]);
}
os.close();
InputStream is = new FileInputStream(f);
int size = is.available();
for(int i= 0 ;i<size;i++){
//System.out.println(is.read()+” “);
}
char c;
int s = is.available();
for(int x= 0;x<s;x++){
c = (char) is.read();
System.out.println(c);
}
}catch(Exception e){ System.out.println(“file not exist”);
}}}
OUTPUT
Enter 3 number :
66
67
68
B
C
D
3) Write a program that takes string as input and write in file1.Copy the contents of file2 to file1 and print the contents of file1.
Input:
package l2t3;
import java.io.*;
import java.util.Scanner;
public class L2t3 {
public static void main(String[] args) {
File f;
File f2;
Scanner sc = new Scanner(System.in);
try{
byte m[] = new byte[3];
System.out.println(“Enter 3 number : “);
for(int i = 0; i < m.length ; i++){
m[i]= sc.nextByte();
}
f=new File(“C:/sss.txt”);
f2=new File(“C:/aaa.txt”);
OutputStream os = new FileOutputStream(f);
OutputStream oss = new FileOutputStream(f2);
for(int i =0;i<m.length;i++){
os.write(m[i]);
}
InputStream is = new FileInputStream(f);
InputStream iss = new FileInputStream(f2);
int size = is.available();
for(int i= 0 ;i<size;i++){
oss.write(is.read());
System.out.println(iss.read()+” “);
}
}catch(Exception e){ System.out.println(“file not exist”);
}}}
OUTPUT:
Enter 3 number :
67
68
69
67
68
69
HOME TASK
1) Write a program that reads names from the given file “Names” and search for a specific initial letter in the names and write a new file “New Names” with these searched names.
Input:
package l2h1;
import java.io.*;
import java.util.Scanner;
public class L2h1 {
public static void main(String[] args) {
File f;
File f2;
char c,d,g;
Scanner sc = new Scanner(System.in);
System.out.print(“Enter initial letter to search for : “);
char v = sc.next().charAt(0);
try{
f=new File(“E:/names.txt”);
f2=new File(“E:/newnames.txt”);
InputStream is = new FileInputStream(f);
InputStream iss = new FileInputStream(f2);
OutputStream oss = new FileOutputStream(f2);
int size = is.available();
char str[] = new char[size];
System.out.print(“Names in file1 : “);
for(int i= 0 ;i<size;i++){
c = (char) is.read();
System.out.print(c);
str[i] = (char) c ;
}
System.out.println(“”);
for(int k = 0 ; k<size ; k++){
if(str[k] == v){
for(int j = 0; j < 6 ; j++){
oss.write(str[j]);
}
}}
int size2 = iss.available();
System.out.print(“Names in file2 : “);
for(int k = 0;k<size2;k++){
g = (char) iss.read();
System.out.print(g);
}}catch(Exception e){ System.out.println(“Either file not exist or Error is found!”);
}
}}
Output:
Enter initial letter to search for : s
Names in file1 : umair
Names in file2 : husssain
2) Write a program that print total number of words in a file.
Input:
package l2h2;
import java.io.*;
import java.util.Scanner;
public class L2h2 {
public static void main(String[] args) {
File f;
String c;
char d;
int words = 0;
Scanner sc = new Scanner(System.in);
try{
f=new File(“C:/names.txt”);
InputStream is = new FileInputStream(f);
int size = is.available();
String str[] = new String[size];
System.out.print(“Data in file1 : “);
for(int i= 0 ;i<size;i++){
d = (char)is.read();
str[i] = Character.toString(d);
if(” “.equals(str[i])){
words++;
}
System.out.print(d);
}
System.out.println(“\nNumber of words in file : “+(words+1));
}catch(Exception e){ System.out.println(“Either file not exist or Error is found!”);
}}}
Output:
Data in file1 : ROSE
Number of words in file : 1