Results 41 to 60 of 63
Thread: FileInputStream question.
- 01-14-2012, 08:22 PM #41
- 01-14-2012, 08:24 PM #42
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: FileInputStream question.
Norm thanks for all the efforts.
We are both wasting our times.
Your way of helping is obviously not helping me, rather confusing me.
I am a simple guy, with simple answers that are asked to me.
and I prefer simple answers myself.
Thanks for all the efforts,
I will try harder and hope to understand how this method could be used..
Maybe as I get better in JAVA, I will..
Thanks.
- 01-15-2012, 06:26 AM #43
- 01-15-2012, 06:53 AM #44
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: FileInputStream question.
It amazes me how far people will go before they try out a method etc. Seems quoting the Buddha is an exempt or something. Funny thing is if he coded up the most awful use of FileInputStream ever seen to man he would have gotten some real code examples or near enough... pains me
- 01-15-2012, 09:23 AM #45
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 01-15-2012, 09:37 AM #46
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: FileInputStream question.
@JosAh thanks, I am aware of this.If you want an InputStream that reads from a byte array (not a file), have a look at the ByteArrayInputStream class. (it is also mentioned in the API documentation as a sub class of the InputStream class).
kind regards,
Jos
But I do not want to read from an array. I am trying to understand what this method would be used for.
@people who are in pain:
http://cbsnewyork.files.wordpress.co...ills.jpg?w=300
-
Re: FileInputStream question.
- 01-15-2012, 06:14 PM #48
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 01-16-2012, 10:31 PM #49
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: FileInputStream question.
Hi fatabass,
I will try to answer your question.
See, you are probably confused due to the returning value of this method. This method is returning an integer, but that is really not what you are after.
Also, what is confusing you is that, the array you are passing as an argument to this method has 2 functions:
1) This method gets the length of the array,
--> This method read that many bytes from a file,
2) This method passes the byte values to the same array you are passing as an argument to this method.
You are probably stuck with the returning int value, because usually read() methods RETURN relevant read information. (like a string, or an integer..) But this is not the case in this method.
So, to your very clear question:
You would use this method to read a file as bytes and store it in an array easily.
Here is a sample code.
You will need a try.txt file in c:\ , and just put some text
This will output the text in the file to the console.Java Code:public static void main(String args[]) throws IOException { FileInputStream firstStream = new FileInputStream(new File("c:/try.txt")); byte[] myArray = new byte[firstStream.available()]; firstStream.read(myArray); for(int i=0;i<myArray.length;i++) { System.out.print((char)myArray[i]); } }
Good luck learning JAVA.
- 01-16-2012, 10:34 PM #50
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: FileInputStream question.
Here in
You are passing an integer value to the method. (Which the length of that array..)Java Code:firstStream.read(myArray);
The method reads that many bytes from the source.
The method writes those bytes to the array you have passed as the argument.
You do not care about the returning value.
- 01-16-2012, 10:44 PM #51
Re: FileInputStream question.
No!!! You do care! That tells you how many bytes were read. It can be important to know in the real world.You do not care about the returning value.
- 01-16-2012, 10:48 PM #52
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: FileInputStream question.
Thank you so much Norm.
I should have said:
You should not focus on the returning value to understand what this method is really doing. The returning value is more like an extra feature of this method. It doesn't have REALLY MUCH about what the method is doing. ( Since what the method really doing is, it gets a byte array, gets its length, reads that many bytes from the source, and passes them as bytes to the same byte array you passed to the argument. )
- 01-16-2012, 10:52 PM #53
Re: FileInputStream question.
Some times it reads fewer and when it does the value returned by the read method will tell you.reads that many bytes from the source,
For reading from disk it is mostly the same. If reading over a network connection, it can be different.
- 01-16-2012, 10:54 PM #54
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
- 01-16-2012, 10:58 PM #55
Re: FileInputStream question.
yes. It should not be ignored.
- 01-16-2012, 11:19 PM #56
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: FileInputStream question.
One more question:
First the information:
IF a character in my text file has a corresponding byte value of 200, which is out of primitive type byte limit, what happens ?There’s a difference between the bytes in a stream and the bytes represented
by the byte class. A byte in Java has a value ranging from –128
to 127, while a byte in a stream has a value from 0 to 255.
Thanks.
- 01-16-2012, 11:26 PM #57
Re: FileInputStream question.
A byte has 8 bits. It can have 256 different values. If the byte is signed, then the top bit is used for the sign which changes the range of numbers it can represent as you describe. If you put the int value 200 into a byte it will have the top bit set on which will make it a negative number.
Try this to see what bits are used for 200:
System.out.println(Integer.toBinaryString(200)); // 11001000
- 01-16-2012, 11:33 PM #58
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: FileInputStream question.
Thanks for the answer,
After this point I am lost..A byte has 8 bits. It can have 256 different values. If the byte is signed, then the top bit is used for the sign which changes the range of numbers it can represent as you describe. If you put the int value 200 into a byte it will have the top bit set on which will make it a negative number.
like the char 'k' has a byte value 107.
So when you read a character 'k' from a text file with FileInputStream, the InputStream returns the value 107 to a byte[] array, and a byte can hold the value 107 without any errors.
But what if reads some character that has a byte value greater than 127? What happens then? ( I wish I knew a such character so I could try, I found this: UTF-8 - Wikipedia, the free encyclopedia, but then the largest value is 122 (of character 'z'), which can be stored as the primitive type byte in JAVA )
- 01-16-2012, 11:39 PM #59
Re: FileInputStream question.
Since Americans were the ones to develop this scheme, they put their letters first in the ASCII characters (0-127).
Most letters in the rest of the world require 2 or more bytes to store their values in.
You need read up on Unicode and how all the different languages get a range of values for their letters. Think of Chinese and Thai and the Indian languages. They all have different ranges of values for their letters.
- 01-16-2012, 11:42 PM #60
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: FileInputStream question.
Thanks,
So it will never need to read a value of 200, because in no character set it represents a character.
If 0-127 is not enough, a weird character like :
(
)
will be represented by two byte values. ( like 66 66 ), and java will just know it should read two of them and return the character corresponding.
Similar Threads
-
FileInputStream
By lakpathy in forum New To JavaReplies: 2Last Post: 01-18-2011, 11:32 AM -
FileInputStream
By lakpathy in forum New To JavaReplies: 4Last Post: 01-17-2011, 12:00 PM -
FileInputStream Problems.
By bayan in forum New To JavaReplies: 18Last Post: 08-11-2010, 11:11 AM -
FileInputStream
By pachufir in forum New To JavaReplies: 3Last Post: 12-11-2009, 05:07 PM -
FileOutputStream > int > FileInputStream
By dudejonne in forum New To JavaReplies: 11Last Post: 11-11-2009, 04:03 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks