Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java error reading from file 
 

Java error reading file are the common error occurred in java that when the user encounter a corrupt file and show a message to the user reason for the failure.

 

Java error reading from file

                         

Java error reading file are the common error occurred  in java that when the user encounter a corrupt file and show a message to the user reason for the failure.

Understand with Example

In this Tutorial we help you in understanding a n example from Java error reading from file. For this we have a class error reading from file. Inside the main method we have a try block that create an instance  of errorreadingfromfile.

1).readfile ( ) -This method is used to read a file.

Try block subsequently followed by a catch block that is used to catch exception

We have  a readline method that include try block declares a variable String  name text is initialized with null.

1)Fileinputstream-The Fileinputstream is used to decode the byte  and convert it into character type.

2)Bufferedinput stream-The Bufferedinputstream is used to read the character from the given text file.

3)Datainputstream     -The Datainputstream allow the application to read from primitive data type from underlying input stream.

4)close ( )                  -This close the filestream.

While evaluate the text, if it is not null, The System.out.println print the text, Otherewise,it move to the catch block to show user defined message" An error occur while reading file".

errorreadingfromfile.java


import java.io.*;

public class errorreadingfromfile {

    public static void main(String[] argsthrows Exception {
        try {
            errorreadingfromfile f = new errorreadingfromfile();
            f.readfile();
        catch (Exception e) {
            System.out.println(e);
        }
    }

    void readfile() {
        try {
            String text = null;
            File file = new File("/home/girish/Desktop/g.txt");
            FileInputStream fileInputStream = new FileInputStream(file);
            BufferedInputStream bufferedInputStream = new 
            BufferedInputStream(fileInputStream);

            DataInputStream dis = new DataInputStream(bufferedInputStream);
            fileInputStream.close();
            while ((text = dis.readLine()) != null) {

                System.out.println(text);
            }
        catch (Exception ex) {
            System.out.println("An Error occurs while reading file");
        }
    }
    }


Output

Compiling source file to /home/girish/NetBeansProjects/errors/build/classes
Note: /home/girish/NetBeansProjects/errors/src/
errorreadingfromfile.java uses or overrides a deprecated API.

Note: Recompile with -Xlint:deprecation for details.
compile-single:
run-single:
An Error occurs while reading file
BUILD SUCCESSFUL (total time: seconds)


Download code

                         

» View all related tutorials
Related Tags: java c mac process io virtual vi number this sso ai processor to proc e il section machine ce in

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.