Powered by Blogger.
Showing posts with label IO Package. Show all posts
Showing posts with label IO Package. Show all posts

Read a file using BufferedReader and Scanner in Java

>> Thursday, July 2, 2015

Being a java developer, will get a chance to work with files using java.util and java.io packages. So mostly we use BufferedReader and Scanner Classes to read different files. These classes will contain respective methods to read files. Here, we will go through the classes and their's differences with a simple java program.
Read a file using BufferedjReader and Scanner_JavabynataraJ

Read more..

Program to Convert Decimal to Octal,Hexa and Binary values

>> Saturday, October 18, 2014

Binary, Hexa and Octal are the number formats to use in different mathematical calculations. In our real life we use numbers with decimals. It may be integer value or float value both are called as decimals. Let us know the number format examples given below:
Decimal format : A representation of a real number using the base ten and decimal notation.
program to convert decimal to octa-hexa-binary_JavabynataraJ

Read more..

program to read/find emails from the given text file

>> Sunday, September 7, 2014

Today morning i need to collect some emails of java guys from the java fourms or websites. So i started copying emails in this page by slecting the email and copying using Ctrl+C, i have done 20 emails copied. Then i lose my patience to copy. Immediately i got an idea, why can't i write a program to read all the emails from the page. Then i copied all the text from the page and pasted in a text file named as emails.txt. Now the java program came in to the picture and we should use java.io package to write or read file operations. In addition i am taking java.util.HashSet and java.util.regex packages to start our program. Here we are reading a text file containing emails with text and writing into another text file this is called deserialization and serialization.
find emails from the given text file_JavabynataraJ

Read more..

Object Serialization and Deserialization

>> Friday, May 6, 2011

Working with ObjectOutputStream class and ObjectInputStream Classes

Object Serialization and Deserialization_JavabynataraJ

When ever the object values need to be stored with in a file then ObjectOutputStream class is used. When ever the object values need to be retrieved from a file then ObjectInputStream class is used.

When ever the object values need to be stored with in a file or need to be retrieved from a file then class corresponding to that object must implement the interface Serializable.

Read more..

8). Program for reading the data from Zipped file using java

>> Wednesday, May 4, 2011

Uncompress the data from a zipped file using java.


 To Convert the contents present in zip_hello file to the original data then the class InflatorInputStream is used.This class is used to convert unreadable format data to the readable format.


InflaterInputStream iis = new InflaterInputStream(fis);


While creating the object of this class it expects FileInputStream class object (fis) as an argument.

InflatorInputStream class is available in java.util.zip package.

This class implements a stream filter for uncompressing data in the "deflate" compression format. It is also used as the basis for other decompression filters, such as GZIPInputStream.

Read more..

7). Program to Zipping the contents present with in the file.

>> Tuesday, May 3, 2011

Making a file into unreadable format.


To Zip the contents present with in a file the class DeflatorOutputStream is used.This class is defined with in the util package.

DeflaterOutputStream dos = new DeflaterOutputStream(fos);

The Object of DeflatorOutputStream class will expect FileOutputStream object as an argument.

Read more..

Copying the data from one file to another file using IO Streams.

>> Friday, April 29, 2011

Copying the data from one file to another file using IO Streams_JavabynataraJ
Here the task is we have to understand  Copying the data from one file to another file.So the data should be serialized first and then by doing deserialization of same file and again serialize with different file name.



1). Write the data into a file "hello2.dat".
FileOutputStream fos = new FileOutputStream("hello2.dat");
     byte b[] = str.getBytes();
     fos.write(b);
     fos.close();

Read more..

5 ).Write a program to read a file by sending the argument as a file name at runtime.

>> Thursday, April 28, 2011

Here we are using BufferedReader to read the name of the file.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


Read the file using FileInputStreamReader then send it to BufferedInputStreamReader to read all the data with InputStreamReader provided mehtods.



package javabynataraj.iopack;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileArgs {
    public static void main(String[] args)throws IOException {
        int ch;
        String fname;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter file name: ");
        fname = br.readLine();
        FileInputStream fis = new FileInputStream(fname);
        BufferedInputStream bis = new BufferedInputStream(fis);
        while((ch=bis.read())!=-1){
            System.out.print((char)ch);
        }
        fis.close();
        bis.close();

    }
}


Note:
You should have one serialized file or any file to enter the name at runtime .you should enter filename with extension like ' hello.dat '.

Read more..

4 ). Program to persist the data into a file using BufferedOutputStream

>> Wednesday, April 27, 2011

#1). Buffered classes are used to increase the performance of the application.

BufferedOutputStream working flow
#2). When ever the Buffered Classes are used by default a small amount of memory will be created with the size of 512 bytes.

#3). The data need to be written into a file, first data will be stored in the buffer then the entire data will be dumped to a file.(present with in the buffer).

#4). For each FileInputStream class there will be associated BufferedInputStream class.

String str="hi this is muralidhar";
     FileOutputStream fos=new FileOutputStream("hello.dat");


#5). These BufferedStream classes will take the other stream classes objects as an arguments.


BufferedOutputStream bos = new BufferedOutputStream(fos);


read the file and save into array of bytes  using getBytes( ) method.

and write the  object into the bos object.


package javabynataraj.iopack;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedOutput {
    public static void main(String[] args) {
        String str="hi this is muralidhar";
        try{
            FileOutputStream fos=new FileOutputStream("hello.dat");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            byte by[] = str.getBytes();
            bos.write(by);
            bos.close();
            fos.close();
            System.out.println("Done........!");
        }
        catch(IOException ioe){
            ioe.printStackTrace();
        }
    }
}



Read more..

3). Write a program to write the data into a file by using FileOutputStream reader

By using FileOutputStream class we have to create an object by passing the argument as a file.

Then read the content in a file using getBytes[] method.


obj.getBytes[];


then by using FileOutputStreamReader class object write the data of bytes array object.

Check out the below code for this program.


package javabynataraj.iopack;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutput {
    public static void main(String[] args) {
        String str = "Hello how are you";
        try{
            FileOutputStream fos = new FileOutputStream("stud.dat");
            byte b[] = str.getBytes();
            fos.write(b);
            fos.close();
            System.out.println("Hello World");
        }catch(IOException ioe){
            ioe.printStackTrace();
        }
    }
}


Read more..

2). Streams vs Reader & Writer

>> Friday, April 22, 2011

Streams

To Write the data into file OutputStreamReader and  to read the data from a file, the class InputStreamReader will be used.

InputStream classes are used to read the data.

OutputStream classes are used to write the data.

The above all defined classes are byte oriented classes

Reader & Writer

The Character oriented classes are the reader and writer classes.These are also defined in an IO Package.These are also the abstract classes.

FileReader class comes under the InputStreamReader class.

FileWriter class comes under the OutPutStreamWriter class.

To Identify the difference between Byte oriented classes and Character oriented class is..
 
  #1). If the class name ends with the word Stream then it is a Byte Oriented class

  #2). If the class name ends with the word Reader or Writer then it is a Character oriented class.

Read more..

1). Introduction to Streams

A Stream represents flow of data mainly streams are used to move the data from input device to output device,or a file or file to output device or file to file.

When ever data need to be transferred then a user need to have the address of the source and the address of the destination. To get the address of the source or destination , a user can make use of some of the Stream classes.

Example:
   
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


InputStreamReader Class will return the address of the standard input device and this class takes an argument as System.in

System is a class which contains a static variable in which represents the standard input device like keyboard.

BufferedReader class Object expects the object of InputStreamReader class as an argument which contains the address of the input device.


Read more..

What is Deserialization in java ? Write a simple program using Deserialization?

>> Monday, April 18, 2011

Deserialization is a process of converting the data from files or database converting to Stream of bytes using class Objects.


The Deserialization can be done after serializing the data only.Then we can read the data from the serialized files.

Convert your Serialized file into file form.

File fromFile = new File("Emp.ser");

By using the below two classes we can do the process of deserialization.
#1. FileInputStream
#2. ObjectInputStream
A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.
FileInputStream fis = new FileInputStream(fromFile);

An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream. This can be Converts the Serialized file into the Object form.
ObjectInputStream ois = new ObjectInputStream(fis);

To get the object of a serialized file we have to typecast into our class Employee to read the values in a file using variable names.
Employee emp = (Employee) ois.readObject();

After that close all the object connections.
You should write the  Employee class already written in Serialization.(check it there) 
Program for Deserialization:
package javabynataraj.iopack;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Deserial {
    public static void main(String arg[]) {
        File fromFile = new File("Emp.ser");
        try {
            FileInputStream fis = new FileInputStream(fromFile);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Employee emp = (Employee) ois.readObject();
            System.out.println("Deserialized data: \n"+ emp.eno + " "+ emp.ename +" "+ emp.esal+" "+emp.eaddr+ "  from Emp.ser");
            ois.close();
        } catch(IOException e) {
            System.out.println("Deserialization failed");
            System.out.println(e);
            System.exit(1);
        } catch(ClassNotFoundException e) {
            System.out.println("Deserialization failed");
            System.out.println(e);
            System.exit(1);
        }
    }
}

Read more..

What is Serialization? Write a Simple program in java using Serialization?

>> Saturday, April 16, 2011

"Serialization is the process of converting the data(Objects) into stream of bytes and storing in to the files or database."

Serialization in Java with Simple program_JavabynataraJ

We can do the Serialization process by implementing Serializable interface.

class SerialDemo implements java.io.Serializable.

The above step tells the compiler that you are going to implement serialization.
Which basically means that the object is going to be saved or persisted.

Read more..

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.