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...

Regarding class files

Class files:- Regarding class file or dot class file also known as byte code of class, in a java programming language we can use any number of classes in our program. After compilation all classes convert into individual   .class   file. Example:- class A { } class B { } class C { } class Student { public static void main(String[]args) { System.out.println("Well-come to my java blog"); } } We can save above program to any   classname.java   or we can use any other   name.java,   there is no compulsion for java file name. But recommended to save by that class name which having main method. I saved the above file to   Student.java   name .   Before compilation my   C:\JAVATECH   folder contain only one java file. Student.java For  compilation. C:\JAVATECH>javac  Student.java After compilation   my   C:\JAVATECH   folder contains five files, on...

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...