Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By fatabass

Thread: Why can't I read all the bytes from a text file ?

  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Why can't I read all the bytes from a text file ?

    Java Code:
    	public static void main(String[] args) throws IOException 
    	{
    		File myTrytxt = new File("c:/try.txt");
    		
    		InputStream myInputStream = new FileInputStream(myTrytxt);
    		
    		System.out.println("Number of bytes:" + myInputStream.available());
    		
    	
    		for(int i = 0; i<myInputStream.available(); i++)
    		{
    			System.out.print((char)myInputStream.read());
    		}
    		
    		myInputStream.close();
    	}
    In try.txt I have 1234.

    When I run this code I get:

    Number of bytes:4
    12

    Why can I read only 1 and 2 ?

    Where is 3 and 4?

    Thanks.

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Why can't I read all the bytes from a text file ?

    When I do this I have no problems:

    System.out.print((char)myInputStream.read());
    System.out.print((char)myInputStream.read());
    System.out.print((char)myInputStream.read());
    System.out.print((char)myInputStream.read());

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Why can't I read all the bytes from a text file ?

    Answering my own question:
    Java Code:
    	public static void main(String[] args) throws IOException 
    	{
    		File myTrytxt = new File("c:/try.txt");
    		
    		InputStream myInputStream = new FileInputStream(myTrytxt);
    		
    		System.out.println("Number of bytes:" + myInputStream.available());
    		
    		int k = myInputStream.available();
    	
    		for(int i = 0; i<k;i++)
    		{
    			System.out.print((char)myInputStream.read());
    		}
    		
    		myInputStream.close();

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Why can't I read all the bytes from a text file ?

    Do you know why, though?

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Why can't I read all the bytes from a text file ?

    Not really..

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,894
    Rep Power
    25

    Default Re: Why can't I read all the bytes from a text file ?

    Add a println first thing in the loop (about line 11) in the first code that prints out the value of i and the value of myInputStream.available().
    The output should show you what is happening.

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Why can't I read all the bytes from a text file ?

    Thanks Norm.
    I guess as my int i's value gets higher, the availablebytes get lower.. so after int 2, int i = 2, and availablebytes = 2..
    Out of the loop..
    Tolls likes this.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,894
    Rep Power
    25

    Default Re: Why can't I read all the bytes from a text file ?

    You got it.

Similar Threads

  1. How to write bytes to text file?
    By fatabass in forum New To Java
    Replies: 10
    Last Post: 09-18-2012, 08:46 PM
  2. Replies: 2
    Last Post: 03-25-2011, 03:36 PM
  3. How to read first 9 bytes and write in...?
    By aRTx in forum New To Java
    Replies: 8
    Last Post: 03-26-2009, 03:54 PM
  4. Replies: 5
    Last Post: 10-17-2008, 03:13 PM
  5. DES algorithm (Read and Write bytes to file)
    By JoaoPe in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 04:46 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
  •