Results 1 to 6 of 6

Thread: Exception in java input/output

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Exception in java input/output

    Hello everyone,
    Am I required to handle exceptions that is the try and catch block every time I use DataInputStream methods to access files? I use eclipse IDE and I have done input output program in java, but I have not included exception in my code, and they still work. Is it compulsory to use exception in java input output. Here is my code.
    Code:
    	Public static void hand(String[] args) {
    
    		FileInputStream fl = new FileInputStream("data.dat");
    DataInputStream stream = new DataInputStream(fl);
    		
    		boolean end = false;
    		int val;
    		while (! end){
    			val = flow.readInt();
    			if (val! = -1)
    			{
    				System.out.print(worth);
    			}
    			else
    			{
    				= endtrue;
    System.out.print("End of File");
    fl.close();
    			}						
    		}			
    	}
    }

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Exception in java input/output

    Hello,
    Yes we must put them. Inn your program you must place them and do not forget the finally block to close the stream properly. You can find tutorial on java.io and the use of exception in them. It is always a good practice to use exception in java input and output as you are dealing with file and file system. If you want to know where and how to place the exception in the code then post your full code and we will guide you with the same.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Exception in java input/output

    Hi,
    Am I required to handle exceptions that is the try and catch block every time I use DataInputStream methods to access files?
    It depends on the type of the program you are writing
    - YES if you want to do a clean code and robust application
    - NO if you can afford to crash nastily if your application problem.
    But it is always better if you use the exception, rather I recommend to use the exception in all java programs because they are helpful in debugging the programs later. And by using the exception you can guide the user to the correct error or in the correct direction.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Exception in java input/output

    Hi.
    It is better to close the stream in a try / finally block and catch block to be sure to close whatever happens, see the code below for how to use finally block and the others.
    Code:
       try {
                / / Read data from a file on disk
                DataInputStream  fl = new DataInputStream(new FileInputStream("data.dat"));
                try {
                    
                    / / Process here
                    
                } finally {
                    / / Close the streams
                    fl.close();
                }
            } catch (IOException e) {
                / / Process exceptions
                e.printStackTrace();        
            }

  5. #5
    Join Date
    Dec 2009
    Posts
    213

    Re: Exception in java input/output

    Hello,
    Here is my modified code, please guide me, where I have to use the try and catch block in my code
    Code:
    Public class ioexam {
     
    	Public static void hand(String[] args) {
    		try {
    				
    			
    			FileInputStream fl = new FileInputStream("/ home / loic / PROJECTS / ioexam / data.dat");
    DataInputStream st = new DataInputStream(fl);
    			
    			boolean ed = false;
    			int val;
    			try{
    			while (! ed){
    				val = flow.readInt();
    				if (val! = -1)
    				{
    					System.out.print(val + " ");
    				}
    				else
    				{
    					ed =true;
    System.out.print("End of File");
    fl.close();
    				}
    			}}catch(EOFException eof) {flow.close();}
    			}catch (IOException e) {System.out.System.out.println("File access error");}
    	}
    }

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    Re: Exception in java input/output

    Hello,
    The try / finally and catch block is one of the most important to have a secure code. If your output is about normal that is some time your code displays undesired results, DataInputStream reads raw data and readInt () reads an int, i.e 4 bytes. If you read a text file this result is quite normal, so it is a good practice if you make use of the try catch and the finally block in your code.

Similar Threads

  1. Input /Output error while using SanDisk USB Flash Drive
    By Mayoor in forum Portable Devices
    Replies: 5
    Last Post: 07-09-2011, 10:39 PM
  2. What is Input Output Operator in C++?
    By Shiva$m in forum Software Development
    Replies: 4
    Last Post: 29-12-2010, 02:33 AM
  3. Input and output arguments in java
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 09-03-2010, 12:32 PM
  4. How to Optimize memory or ram: input / output
    By fumble in forum Tips & Tweaks
    Replies: 0
    Last Post: 21-01-2009, 07:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,259,331.44569 seconds with 17 queries