Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • File handling basics in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 366
    Comment on it

    Hi there,

    This blog is to get you familiarize with Java file handling using java.io package. Suppose you need to read the content of your file and need to do operation on that data, in that situation you can opt to use file handling to access the data of that file.

     There are two types of stream

    • InPutStream

    • OutPutStream

     

    InPutStream is used to read data from source and OutPutStream used to write data to a destination.

    In this example we are going to use Byte Stream which used to perform input and output of 8-bit. To read the data of a file we will use FileInputStream class and to write FileOutputStream respectively.

    Now we do the read operation on the content of file given below.

     

    Name of my file is data.txt

     

    What is Lorem Ipsum?
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    
    Why do we use it?
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

    A Java program to read the content of your data.txt file using byte stream.

     

    FileReadDemo.java

     

    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class FileReadDemo {
    public static void main(String[] args) throws IOException {
     FileInputStream in = null;
     in = new FileInputStream("data.txt");
     int c;
    while ((c = in.read()) != -1) {
    System.out.print((char)c);
    }
    in.close();
    }
    }
    

    put the data.txt file and FileReadDemo.java under the same directory, run the above program an the output will be printed on console.

    Now the following program is to read the content of a file and write it in an another file created at run time with name output.txt.

     

    FileReadWriteDemo.java

     

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class FileReadWriteDemo {
    public static void main(String[] args) throws IOException {
     FileInputStream in = null;
     FileOutputStream out = null;
     in = new FileInputStream("data.txt");
     out = new FileOutputStream("output.txt");
     int c;
    while ((c = in.read()) != -1) {
    System.out.print((char)c);
    out.write(c);
    }
    in.close();
    out.close();
    }
    }

    after running the above program the content of data.txt file will be copied in a newly created file named as output.txt with content of data.txt copied into it.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: