Q.1 What Is File? Explain Functions of File With Explanation and Example. Java - Io Package File Class
Q.1 What Is File? Explain Functions of File With Explanation and Example. Java - Io Package File Class
RandomAccessFile r =
Q.4 Explain RandomAccessFile with new RandomAccessFile(args[0],"r");
Example.
String str;
• A FileInputStream can be used to read from a r.seek(10);
file, and a FileOutputStream can be used to System.out.println(r.length());
write to a file.
• We may be interested in reading and writing System.out.println(r.getFilePointer());
to the same file. while((str=r.readLine())!=null)
• For this, we have a class called System.out.println(str);
RandomAccessFile which can be used for
reading and writing to the file. System.out.println(r.getFilePointer());
• RandomAccessFile implements Closable, }
DataInput and DataOutput interface. }
class TestEmployee
{
public static void main(String []args)
throws Exception
{
Employee e1 = new Employee(101,"Ram",590);
Employee e2 = new Employee(102,"Sita",470);
Employee e3 = new Employee(103,"Lax",450);
//Serialization
ObjectOutputStream oos = new
ObjectOutputStream(new
FileOutputStream(args[0]));
oos.writeObject(e1);
oos.writeObject(e2);
oos.writeObject(e3);
oos.flush(); ________________________________________________________
oos.close(); ________________________________________________________
________________________________________________________
//Deserialization ________________________________________________________
ObjectInputStream ois = new ________________________________________________________
ObjectInputStream(new ________________________________________________________
FileInputStream(args[0])); ________________________________________________________
Employee e4 = (Employee)ois.readObject(); ________________________________________________________
Employee e5 = (Employee)ois.readObject(); ________________________________________________________
Employee e6 = (Employee)ois.readObject(); ________________________________________________________
System.out.println(e4+""+e5+""+e6); ________________________________________________________
} ________________________________________________________
} ________________________________________________________
Q.6 Write a code to copy content from one file Q.8 Write a Java code to display content of
to another file. file with line number and display only even
lines of given file.
import java.io.*; import java.io.*;
import java.util.*;
class CopyFile class FileReadDemo3{
{ public static void main(String[] args) throws
public static void main(String []args) Exception {
throws Exception FileInputStream fis = new
{ FileInputStream(args[0]);
InputStream i = LineNumberReader lr = new
new FileInputStream(args[0]); LineNumberReader(new InputStreamReader
(fis));
byte[] b = new byte[i.available()]; String line;
i.read(b); while((line=lr.readLine())!=null){
FileWriter f = new FileWriter(args[1]); if(lr.getLineNumber()%2==0)
f.write(new String(b)); System.out.println(lr.getLineNumber()+" :”
f.close(); +line);
} }
} fis.close();
}
Q.7 Write a code to read content from file. }
Q.9 Explain Thread Life Cycle with Example after calling resume or after completion of sleep
Program. OR Explain Thread States. thread will be again in Runnable State.
class Test extends Thread DEAD State: After completion of run() method,
{ Thread will be in Dead State.
Test()
{ ________________________________________________________
super(); // NEW or BORN ________________________________________________________
start(); ________________________________________________________
} ________________________________________________________
//Runnable State ________________________________________________________
public void run() ________________________________________________________
{ ________________________________________________________
try{ ________________________________________________________
Thread.sleep(5000); // BLOCKED ________________________________________________________
}catch(InterruptedException e){} ________________________________________________________
} ________________________________________________________
//DEAD ________________________________________________________
} ________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
Q.12 Explain Use of join() method with Q.13. Differentiate Daemon Thread Vs
Example. Non-Daemon Thread
• The join() method waits for a thread to die. Daemon Thread Non-Daemon
In other words, it causes the currently Thread
running threads to stop executing until the These threads are generally These threads are
thread it joins with completes its task. known as a "Service generally known as a
• Syntax Provider" thread. "User thread".
o public void join() throws These threads should not These threads are
InterruptedException be used to execute program created by
o public void join(long milliseconds) code but system code. programmer. It
throws InterruptedException generally meant to
run program code.
class Test extends Thread These thread run in parallel JVM doesn't
{ to your code but JVM can terminates unless all
Test(String name) kill them anytime. When the user thread
{ JVM finds no user threads, terminate.
super(name); it stops and all daemon
start(); thread terminate instantly.
} We can set non-daemon We can set daemon
public void run() thread to daemon using thread to non-
{ setDaemon(true) daemon using
for(int i=1;i<=5;i++){ System.out.println(i+" : setDaemon(false)
"+this);
try{
Thread.sleep(1500); ________________________________________________________
}catch(Exception e){} ________________________________________________________
} ________________________________________________________
} ________________________________________________________
} ________________________________________________________
class TestDemo ________________________________________________________
{ ________________________________________________________
public static void main(String []args) ________________________________________________________
{ ________________________________________________________
Test t1 = new Test("One"); ________________________________________________________
Test t2 = new Test("Two"); ________________________________________________________
Test t3 = new Test("Three"); ________________________________________________________
try{ ________________________________________________________
t1.join(); ________________________________________________________
t2.join(); ________________________________________________________
t3.join(); ________________________________________________________
}catch(Exception e){} ________________________________________________________
System.out.println("Main Exit"); ________________________________________________________
} ________________________________________________________
} ________________________________________________________
________________________________________________________
Main Thread will wait for all child thread to ________________________________________________________
finish. ________________________________________________________
________________________________________________________