Skip to main content

File Handling(I/O-2)

Example6:- TestFileOutputStream.java(byteoriented stream)

import java.io.*;
class TestFileOutputStream
{
public static void main(String[]args)throws IOException
{
FileOutputStream fout=new FileOutputStream("abc2.txt");
fout.write(97);
String s="well-come to javaindore";
byte[]b=s.getBytes();
fout.write(b);
}
}
Output:-
awell-come to javaindore

Example7:-DataFile.java

import java.io.*;
class DataFile
{
public static void main(String[]args)throws IOException
{
FileOutputStream fout=new FileOutputStream("book.txt");
DataOutputStream ob=new DataOutputStream(fout);
ob.writeInt(101);
ob.writeBoolean(true);
ob.writeChar('A');
ob.writeBytes("hello javaindore");
}
}
Output:-
   e Ahello javaindore

Example8:- TestReader.java

import java.io.*;
class TestPrintStream
{
public static void main(String[]args)throws IOException
{
FileOutputStream fw=new FileOutputStream("abc3.txt");
PrintStream fout=new PrintStream(fw);

fout.write(97);
fout.println('A');
fout.println(10);
fout.println(32767);
fout.println(2147483647);
fout.println(2147483699L);
fout.println(10.80F);
fout.println(10.90);
String s="well-come to javaindore";
byte b[]=s.getBytes();
fout.write(b);
}
}
Output:-
aA
10
32767
2147483647
2147483699
10.8
10.9
well-come to javaindore

Example9:- FileObject.java

import java.io.*;
class FileObject implements Serializable
{String name;
int roll;
public static void main(String[]args)throws IOException
{
FileObject f=new FileObject();
f.name="Sona";
f.roll=101;
FileObject f1=new FileObject();
f1.name="Sona123";
f1.roll=10101;
FileOutputStream fr=new FileOutputStream("obj.txt");
ObjectOutputStream ff=new ObjectOutputStream(fr);
ff.writeObject(f);
ff.writeObject(f1);
}
}

Example10:- FileObject.java

import java.io.*;
class FileObject implements Serializable
{String name;
int roll;
public static void main(String[]args)throws IOException
{
FileObject f=new FileObject();
f.name="Sona";
f.roll=101;
FileObject f1=new FileObject();
f1.name="Sona123";
f1.roll=10101;
FileOutputStream fr=new FileOutputStream("obj.txt");
ObjectOutputStream ff=new ObjectOutputStream(fr);
ff.writeObject(f);
ff.writeObject(f1);
FileInputStream fin=new FileInputStream("obj.txt");
ObjectInputStream o=new ObjectInputStream(fin);
try
{
FileObject b=(FileObject)o.readObject();
while(b!=null)
{
System.out.println(b.name+"        "+b.roll);
b=(FileObject)o.readObject();
}
}
catch(Exception e){}
}
}
Output:-
C:\JAVATECH>javac FileObject.java
C:\JAVATECH>java FileObject
Sona        101

Sona123        10101

Comments

Popular posts from this blog

Inheritance-4

Example10:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(null); System.out.println("This is sub class Zero argument constructor"); } public static void main(String[]args) { Test t=new Test(); } } Output:- C:\JAVATECH>javac Test.java C:\JAVATECH>java Test This is super class One argument constructor This is sub class Zero argument constructor Example11:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(); super(null); System.out.println("This is su...

inheritance

INHERITANCE:- Inheritance is the process of code re-usability, using this property software or applications can develop in less time or can develop rapidly. Inheritance increases the readability of code and decrease redundancy or repetition of code or reduces size of code. Use of previously developed code in future any number of times is called inheritance. Through inheritance variables and methods of class can inherited. Types of Inheritance:- 1)         Single or Single level inheritance 2)         Multilevel inheritance 3)         Hierarchical inheritance Java does not support to multiple inheritance. Single or single level inheritance:- In a single inheritance a class is inherited by a single class. In a single or single level inheritance a sub class having only one super class directly or indirectly. Syntax: -        class Subclass ex...

Abstraction,Encapsulation And Inheritance

Abstraction  is a one of the great feature of Oops,in this feature hide entire details of product, show only requirement specifications(what product will do). The main purpose behind abstraction is to increase productivity or use of product without knowledge of product implementation. in this case the person don't know how product  was implemented but know how product works or action of product. In java abstraction is achieved by abstract class and interface,here interface provide 100% abstraction and abstract class provide partial abstraction. For example: - ATM machine is example of abstraction.  every technical and non-technical person use facility of ATM without knowing the entire process. they know if they click on withdraw button then they can withdraw money from ATM machine but they don't know when they click on withdraw button the request is send to the server and after go into database and so on. Here ATM machine show only requirement specificat...