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

Inner classes

Inner Classes:- class inside class or interface called inner class or nested class. Advantage of inner classes is grouping multiple classes and interfaces into a single class. Another big advantage of inner class is that inner class can happily access all the data or variables of outer class directly. Types of Nested classes:- There are two types of nested classes static and non-static nested classes. (A)Non –static inner classes:- 1)     Member inner class 2)     Anonymous inner class 3)     Local inner class (B)Static nested class Example1:-Outer.java class Outer { class Inner { void disp() { System.out.println("Hello This is Inner class Method"); } } public static void main(String[]args) { Inner in=new Inner(); in.disp(); } } Output:- C:\JAVATECH>javac Outer.java Outer.java:12: error: non-static variable this cannot be referenced from a static context Inner in=new Inner(); ...

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

Small concept about OOPs

Some small concept about OOPs:- In many books and website write about OOPs concepts as hundred percent object oriented, pure object oriented and partial object oriented language. Hundred percent object oriented:- In the hundred percent object oriented movie hero is object, without object program is incomplete that means can not execute program without object. Without class and object programming or application development is not possible. Example of hundred percent object oriented language is  Smalltalk . Pure object oriented:- In the object oriented cinema obviously object is the hero. In pure object oriented language object having same importance but in this having some relax that is without creating object we can successfully execute our application. So in the object oriented programming without class we cannot create or run our application, class is must. Example of pure object oriented language is  Java . Partial object oriented:- In this type of la...