You are on page 1of 9

LAB ASSIGNMENT -4

NAME:ADITH KUMAR MENON


REGISTRATION NUMBER:18BCE2311

Q.Write a program to enter your name, address, Regd. No., No. of siblings into a text
file and retrieve them from the file and print them by using both the IO streams (Byte
Stream and character stream)

Byte Stream:
import java.io.*;
import java.nio.charset.StandardCharsets;

public class byte3 {

public static void main(String[] args) throws IOException {

String name = "";


String addr = "";
String REGNO= "";
String sib_no="";
BufferedOutputStream out = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(new
File("C:\\adith\\myFile1.txt"));
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
out = new BufferedOutputStream(fileOutputStream);

System.out.print("Please enter your name: ");

name = in.readLine();

System.out.println("Hello, " + name + "!");

System.out.print("Please enter your address: ");

addr = in.readLine();

System.out.println("addr is, " + addr + "!");

System.out.print("Please enter your REGNO: ");

REGNO = in.readLine();

System.out.println("REGNO" + REGNO + "!");

System.out.print("Please enter NUMBER OF SIBLING: ");


sib_no = in.readLine();

System.out.println("Number of sibling " + sib_no + "!");

out.write(name.getBytes());
out.write(addr.getBytes());
out.write(REGNO.getBytes());
out.write(sib_no.getBytes());

System.out.println("Writing data into a file is success!");

}
catch(Exception e) {
System.out.println(e);
}
finally {
out.close();
}
}
}

OUTPUT:

TXT FILE:
Character Stream:

THESE ARE THE DATA THAT WILL BE PRESENT IN .TXT FILE

import java.io.*;
import java.nio.charset.StandardCharsets;

public class character3 {

public static void main(String[] args) throws IOException {

Writer out = new FileWriter("C:\\adith\\dataFile.txt");


InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);

String name = "";


String addr = "";
String REGNO= "";
String sib_no="";

try {

System.out.print("Please enter your name: ");

name = in.readLine();

System.out.println("Hello, " + name + "!");

System.out.print("Please enter your address: ");

addr = in.readLine();

System.out.println("addr is, " + addr + "!");

System.out.print("Please enter your REGNO: ");

REGNO = in.readLine();

System.out.println("REGNO" + REGNO + "!");

System.out.print("Please enter NUMBER OF SIBLING: ");

sib_no = in.readLine();

System.out.println("Number of sibling " + sib_no + "!");


out.write(name);
out.write(addr);
out.write(REGNO);
out.write(sib_no);
}
catch(Exception e) {
System.out.println(e);
}
finally {
out.close();
}
}
}

OUTPUT:

TXT file
Q. Write a program to create a linked list by using collections. Perform all the
operations like adding and removing a node, print all the values present in the linked
list by iterating over the linked list

Ans:

I created 2 programs one is for adding elements and other is for removing the
elements.

ADDING THE ELEMENTS:

import java.util.*;
public class LINKEDlist1{
public static void main(String args[]){
LinkedList<String> list=new LinkedList<String>();

list.add("MCLAREN");
list.add("FERRARI");
list.add("LAMBORGINI");

list.add(2, "FORD");

//Iterating LinkedList
Iterator<String> iterator=list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
Output:

REMOVING THE ELEMENTS:

import java.util.*;
public class LINKEDlist2{
public static void main(String args[]){
LinkedList<String> list=new LinkedList<String>();

//Adding elements to the Linked list


list.add("MCLAREN");
list.add("FERRARI");
list.add("PORSCHE");
list.add("FORD");
list.add("NISSAN");

list.removeFirst();

list.removeLast();

Iterator<String> iterator=list.iterator();
while(iterator.hasNext()){
System.out.print(iterator.next()+" ");
}

list.remove(1);

System.out.print("\nAfter removing second element: ");


//Iterating LinkedList again
Iterator<String> iterator2=list.iterator();
while(iterator2.hasNext()){
System.out.print(iterator2.next()+" ");
}
}
}

OUTPUT:

Q.Write a program to incorporate all the banking transactions until the user enters
“quit”. All the data will be saved to a file, it can be modified by retrieving it from file
and it will be saved again to the file after any modification. If balance will be less than
the requested withdrawal amount then it will wait for deposit operations until the
available balance becomes more than the requested withdrawal amount using multi
threading

ANS:
Code:

import java.util.*;
import java.io.*;

class Balance extends Thread {


int balance;

//displaying the bank balance


public void run() {
try {
File file = new File("C:\\filehandling\\bank.txt");
if (!file.exists()) {
file.createNewFile();
}
Scanner myReader = new Scanner(file);
while (myReader.hasNextLine()) {
balance = myReader.nextInt();
System.out.println(balance);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

class Deposit extends Thread {


int balance;

Scanner KB = new Scanner(System.in);


//method to deposit money
synchronized public void run(){
int amount;
System.out.println("Enter the amount you want to deposit:" );
amount= KB.nextInt();
try {
File file = new File("C:\\filehandling\\bank.txt");
Scanner myReader = new Scanner(file);
while (myReader.hasNextLine()) {
balance = myReader.nextInt();
System.out.println(balance);
}
FileWriter myWriter = new FileWriter("bank.txt");
myWriter.write(balance=balance+amount);
notify();
} catch (IOException e) {
e.printStackTrace();
}

}
}
class Withdraw extends Thread {
int balance;
Scanner KB = new Scanner(System.in);
//method to withdraw money
synchronized public void run(){
int amount;
System.out.println("Enter the amount you want to withdraw:" );
amount= KB.nextInt();
try{
File file = new File("C:\\filehandling\\bank.txt");
Scanner myReader = new Scanner(file);
while (myReader.hasNextLine()) {
balance = myReader.nextInt();
System.out.println(balance);
}
FileWriter myWriter = new FileWriter("bank.txt");
if (balance >= amount) {
System.out.print(balance);
myWriter.write(balance-=amount);

} else {
System.out.print(balance+=200);
System.out.println("\n Thank YOu for making the transaction");

} catch (IOException e) {
e.printStackTrace();
}

public class Transaction{


public static void main (String[] args)
{
Balance obj1 = new Balance();
Deposit obj2 = new Deposit();
Withdraw obj3 = new Withdraw();
obj1.start();
obj2.start();
obj3.start();
}
}

Output:

You might also like