Experiment 12 22z433 2
Experiment 12 22z433 2
Roll No : 22Z433
Date : 06/12/2023
Experiment 12
package EXP_12;
import java.util.Scanner;
char character;
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Enter a character : s
The entered character is : s
Page 1
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
2.
Aim : To write a java program to read a string from the console using
BufferedReader.
package EXP_12;
import java.io.*;
String name;
name = string_reader.readLine();
}
}
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Enter a string: santhosh
You entered santhosh string.
3.
Aim : To write a java program to handle console output using printwriter
class.
Page 2
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
package EXP_12;
import java.io.PrintWriter;
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Using the PrintWriter to print the output..
Hello, World!
4.
Aim : To write a java program to print console output using write( )
// Program to print console output using write()
package EXP_12;
import java.io.PrintStream;
out.write("Hello, World\n".getBytes());
out.write("This is how we use the write() method to print
Page 3
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
console output..\n".getBytes());
}
}
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Hello, World
This is how we use the write() method to print console output..
5.
Aim : To write a java program to read a file using read( ).
package EXP_12;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
int byteRead;
while ((byteRead = inputStream.read()) != -1) {
char character = (char) byteRead;
System.out.print(character);
}
inputStream.close();
Page 4
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
}
}
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
5 1 2 3 4 6 7
6.
Aim : To write a java program to copy a file from one place to another place.
package EXP_12;
import java.nio.channels.FileChannel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
try{
inputStream = new
FileInputStream(source).getChannel();
outputStream = new
FileOutputStream(destination).getChannel();
Page 5
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
outputStream.transferFrom(inputStream, 0,
inputStream.size());
}
catch(Exception e){
System.out.println(e.getMessage());
}
finally{
// Closing the two file channels.
inputStream.close();
outputStream.close();
}
}
}
7.
Aim : To write a java program to use the Automatic Resource Management
(ARM) to close the file.
Page 6
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
package EXP_12;
import java.io.FileInputStream;
import java.util.Scanner;
import java.io.*;
while (scan.hasNextLine()) {
System.out.println(scan.nextLine());
}
scan.close();
}
catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
}
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Hello, I am Santhosh here.
Page 7
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
8.
Aim : To write a java program to work with the arraylist in java collection.
import java.util.ArrayList;
import java.util.Collections;
Page 8
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
}
}
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Red found at : true
black
blue
red
Yellow
9.
Aim : To write a java program to work with LinkedList in java using the
collection framework.
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Iterator;
Page 9
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
animals_list.remove(animals_list.size() - 1);
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Is dog exist : false
lion
elephant
cat
10.
Aim : To write a java program to work with priority queues in java using the
collection framework.
import java.util.PriorityQueue;
import java.util.*;
Page 10
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
public static void main(String[] args){
PriorityQueue <String> subjects = new
PriorityQueue<String>();
subjects.add("Data Structures and Algorithms");
subjects.add("Object Oriented Programming");
subjects.add("Design Patterns");
subjects.add("Distributed Computing");
while (new_iterator.hasNext()) {
System.out.println(new_iterator.next());
}
while(new_Iterator2.hasNext()){
new_subjects.add(new_Iterator2.next());
}
Page 11
Name : T K Santhosh
Roll No : 22Z433
Date : 06/12/2023
Iterator <String> new_Iterator3 = new_subjects.iterator();
while(new_Iterator3.hasNext()){
System.out.println(new_Iterator3.next());
}
}
}
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Data Structures and Algorithms
Distributed Computing
Design Patterns
Object Oriented Programming
Mathematics
No of elements in the priority queue : 5
First element in the priority queue is : Data Structures and
Algorithms
Data Structures and Algorithms
Distributed Computing
Design Patterns
Object Oriented Programming
Mathematics
Result
Hence the given programs were executed successfully.
Page 12