Java Lab Assignment-4
Java Lab Assignment-4
1. Write a program to demonstrate try, catch and finally where code copy content of one file to
another file using byte stream.
Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyExample
{
public static void main(String[] args)
{
FileInputStream instream = null;
FileOutputStream outstream = null;
try
{
File infile =new File("C:\\Users\\admin\\Desktop\\java\\Assignment
4\\MyInputFile.txt");
File outfile =new File("C:\\Users\\admin\\Desktop\\java\\Assignment
4\\MyOutputFile.txt");
instream = new FileInputStream(infile);
outstream = new FileOutputStream(outfile);
byte[] buffer = new byte[1024];
int length;
while ((length = instream.read(buffer)) > 0)
{
outstream.write(buffer, 0, length);
}
instream.close();
outstream.close();
System.out.println("File copied successfully!!");
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
OutPut:
2. Write a program to demonstrate all file related methods.
3. Write a program to read from console and display the same on console using Character
stream.
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
throws IOException
System.out.println(name);
OutPut:
4. Write a program to demonstrate the concept of serialization and deserialization where class
student contain property stud_no, stud_name, subject and marks.
Code:
5. Write java program to read a file and seperate vowels and consonants to other files
Code:
import java.io.*;
try
String str=br.readLine();
char ch;
ch = str.charAt(i);
if(( ch == 'a') ||( ch == 'e') ||( ch == 'i') ||( ch == 'o') ||( ch == 'u'))
out.write(ch);
else
outt.write(ch);
out.close();
outt.close();
br.close();
catch (Exception e)
System.err.println(e);
}
OutPut:
Vowels
Consonants
6. Write a program to demonstrate shallow and deep cloning.
Code:
import java.io.*;
class Test
int x,y;
Test()
x=11;
y=22;
class Main
System.out.println(ob1.x+" "+ob1.y);
Test ob2=ob1;
ob2.x=100;
System.out.println(ob1.x+" "+ob1.y);
System.out.println(ob2.x+" "+ob2.y);
OutPut:
Code:
class Test<T>
T obj;
public T getObject()
return this.obj;
}
class Main
System.out.println(iObj.getObject());
System.out.println(sObj.getObject());
Output:
Code:
class Test
{
System.out.println(element.getClass().getName() +" = " + element);
genericDisplay(11);
genericDisplay("AjinkyaSalunkhe");
genericDisplay(1.0);
OutPut:
9. Write a Java program to show working of user defined Generic functions using Array
Code:
import java.util.*;
class ArrayGenerics
{
public static void main(String args[])
{
ArrayList<String> list=new ArrayList<String>();
list.add("Ajinkya");
list.add("Salunkhe");
String s=list.get(1);
System.out.println("element is: "+s);
Iterator<String> itr=list.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
OutPut:
Code:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
String Email=sc.next();
Matcher matcher=pattern.matcher(Email);
if(matcher.matches())
else
OutPut:
11. Write java program for mobile number validation using regex
Code:
import java.util.regex.*;
import java.util.Scanner;
class MobileNumberValidation
Pattern p = Pattern.compile("(0/91)?[7-9][0-9]{9}");
Matcher m = p.matcher(s);
String s = "9607309819";
if (isValid(s))
System.out.println("Valid Number");
else
System.out.println("Invalid Number");
OutPut:
12. Write java program to accept a string and pattern from user using regex :
Code:
import java.util.regex.*;
class StringPatternUsingRegexExample1
OutPut:
b. number of times pattern is occurring is String.
Code:
import java.util.regex.*;
class StringPatternUsingRegexExample2
System.out.println(temp);
OutPut:
13. Write java program to accept user name and password store it in file. Accept password if
Code:
import java.util.regex.*;
import java.io.FileOutputStream;
import java.util.*;
class PasswordValiadtion
try{
FileOutputStream fout=new
FileOutputStream("C:/Users/admin/Desktop/java/Assignment 4/userid.txt");
FileOutputStream fout1=new
FileOutputStream("C:/Users/admin/Desktop/java/Assignment 4/pass.txt");
String username;
String pass;
username=sc.next();
pass=sc.next();
String regularEx="^(?=.*[0-9])(?=.*[Aa-zZ])(?=.*[@#$%^&-
+=()])(?=\\S+$).{8,}$";
if(ans)
byte b1[]=username.getBytes();
byte b[]=pass.getBytes();
//fout.write("Username:");
fout.write(b1);
//fout.write("Password:");
fout1.write(b);
fout.close();
else
}
}
catch(Exception e)
System.out.println(e);
OutPut:
14. Write java program to split a string by whitespace and display the splitted string [use regex].
Code:
OutPut:
Code:
import java.util.*;
list.add(55);
list.add(34);
list.add(98);
list.add(67);
list.add(39);
list.add(76);
list.add(81);
System.out.println(marks);
Collections.sort(list);
System.out.println(marks);
OutPut:
16. Write a Java program to compare two array lists
Code:
import java.util.*;
System.out.println(firstList);
System.out.println(secondList);
firstList.removeAll(secondList);
System.out.println(firstList);
OutPut:
17. Write a Java program to get the first and last occurrence of the specified elements in a linked
list.
Code:
import java.util.LinkedList;
import java.util.Iterator;
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.add("Pink");
l_list.add("orange");
OutPut:
18. Write a Java program to compare two sets and retain elements which are same on both sets.
Code:
import java.util.*;
h_set1.add("Red");
h_set1.add("Green");
h_set1.add("Black");
h_set1.add("White");
h_set2.add("Red");
h_set2.add("Pink");
h_set2.add("Black");
h_set2.add("Orange");
h_set1.retainAll(h_set2);
System.out.println("HashSet content:");
System.out.println(h_set1);
OutPut:
19. Write a program to print occurrence every word in a sentence using HashMap.
Code:
import java.io.*;
import java.util.*;
class OccurenceOfCharInString
if (charCountMap.containsKey(c))
else
{
charCountMap.put(c, 1);
characterCount(str);
OutPut:
20. Write a Java program to create a reverse order view of the elements contained in a given tree
set.
Code:
import java.util.TreeSet;
import java.util.Iterator;
t_set.add("Red");
t_set.add("Green");
t_set.add("Black");
t_set.add("Pink");
t_set.add("orange");
Iterator it = t_set.descendingIterator();
while (it.hasNext())
System.out.println(it.next());
OutPut: