Results 1 to 8 of 8
- 01-14-2012, 11:49 AM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Why can't I read all the bytes from a text file ?
In try.txt I have 1234.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(); }
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.
- 01-14-2012, 11:52 AM #2
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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());
- 01-14-2012, 11:58 AM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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();
- 01-16-2012, 02:18 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Why can't I read all the bytes from a text file ?
Do you know why, though?
- 01-16-2012, 08:25 PM #5
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Why can't I read all the bytes from a text file ?
Not really..
- 01-16-2012, 08:56 PM #6
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.
- 01-16-2012, 09:01 PM #7
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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..
- 01-16-2012, 09:17 PM #8
Similar Threads
-
How to write bytes to text file?
By fatabass in forum New To JavaReplies: 10Last Post: 09-18-2012, 07:46 PM -
Read a data from a text file and create an object from these data in this text file
By jjavaa in forum Advanced JavaReplies: 2Last Post: 03-25-2011, 02:36 PM -
How to read first 9 bytes and write in...?
By aRTx in forum New To JavaReplies: 8Last Post: 03-26-2009, 02:54 PM -
java.io.IOException: Unable to read entire block; 493 bytes read before EOF; expected
By kushagra in forum New To JavaReplies: 5Last Post: 10-17-2008, 02:13 PM -
DES algorithm (Read and Write bytes to file)
By JoaoPe in forum Advanced JavaReplies: 6Last Post: 07-29-2008, 03:46 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks