Results 1 to 4 of 4

Thread: Java File I/O

  1. #1
    anirbanghosh is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Java File I/O

    I have the following file ...

    INPUT.TXT

    Java Code:
    5
    0 0
    0 1
    0.5 0.5
    1 0
    1 1
    I want to read the numbers...

    Java Code:
    import java.io.*;
    
    
    class ConvexHull
    {
    	public static void main(String args[]) throws IOException
    	{
    	    DataInputStream dataIn;
    		
    	    try 
    	    {
    	    	dataIn = new DataInputStream(new FileInputStream("INPUT.TXT"));
    	    }
    
    	    catch(FileNotFoundException exc) 
    	    {
    	    	System.out.println("File Not Found");
    	    	return;
    	    }
    
    	    BufferedReader inputFileReader = new BufferedReader(new InputStreamReader(dataIn));
    	    
    	    String line;
    	    
    	    line = inputFileReader.readLine();
    	    System.out.println(line);
    	    
    		while (  (line = inputFileReader.readLine()) != null ) 
    		{
    				line = inputFileReader.readLine();
    				
    				System.out.println(line);
    		}
    	    
    	    dataIn.close();
    	    
    	    System.out.println("\nProgram terminated successfully!!");
    
    	}
    }
    I am getting the following output. Please help me out!

    Java Code:
    5
    0 1
    1 0
    null
    
    Program terminated successfully!!

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by anirbanghosh View Post
    Java Code:
                 line = inputFileReader.readLine();
    	     System.out.println(line);
    
    	     while (  (line = inputFileReader.readLine()) != null ) {
    				line = inputFileReader.readLine();
    			        System.out.println(line);
    	     }
    This is where you are messing up the things. You are reading the file contents, twice here in while loop, but printing them only once. This is resulting into skipping the alternate lines of the text file.

    Even you are not supposed to read the text file and print the contents before the while loop.

    Can you now figure out, what needs to be removed in your code?

    Goldest
    Last edited by goldest; 01-17-2011 at 09:05 PM.
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    you can use the Scanner to read your file as well. here's an example
    Java Code:
            Scanner sc = new Scanner( new File("file") );
            String line ;
            while( sc.hasNext() ){
                line = sc.nextLine();
                System.out.println(line);
            }
            sc.close()

  4. #4
    anirbanghosh is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. how to split large xml file into small xml file in java
    By enggvijaysingh@gmail.com in forum XML
    Replies: 2
    Last Post: 02-07-2011, 10:34 AM
  2. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 10:57 AM
  3. Replies: 7
    Last Post: 05-23-2008, 04:46 AM
  4. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM
  5. Converting text file(.txt) to JPG file(.jpg) in java
    By javadeveloper in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 05:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •