Professional Documents
Culture Documents
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;
name = in.readLine();
addr = in.readLine();
REGNO = in.readLine();
out.write(name.getBytes());
out.write(addr.getBytes());
out.write(REGNO.getBytes());
out.write(sib_no.getBytes());
}
catch(Exception e) {
System.out.println(e);
}
finally {
out.close();
}
}
}
OUTPUT:
TXT FILE:
Character Stream:
import java.io.*;
import java.nio.charset.StandardCharsets;
try {
name = in.readLine();
addr = in.readLine();
REGNO = in.readLine();
sib_no = in.readLine();
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.
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:
import java.util.*;
public class LINKEDlist2{
public static void main(String args[]){
LinkedList<String> list=new LinkedList<String>();
list.removeFirst();
list.removeLast();
Iterator<String> iterator=list.iterator();
while(iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
list.remove(1);
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 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();
}
Output: