This week's book giveaway is in the Other Open Source APIs forum. We're giving away four copies of Storm Applied and have Sean Allen, Peter Pathirana & Matthew Jankowski on-line! See this thread for details.
Recently i started learning Java with a nice book. One of the examples uses io stuff in order to read contents of a text file and stores it into an ArrayList instance. Unfortunately, while this runs well on my box, i can't complie this example on another machine running MacOS X(10.2 - panther i think). I guess, it's because the Mac is running Java 1.4.2 - So here's my question : Can i compile this on Windows and make it run on Mac ? (upgrading Java on MacOS X seems even more difficult than on debian ) Another solution i though of was replacing ArrayList with a simle String array, but how can i change the size of the array at run time ?
Thanks for your help
Joined: Sep 27, 2005
so,far the only "solution" i found i this (don't laugh too much, i a newbie )
You are reading the file twice, first time to check the number of lines in the file and second time the actual content, the size of the array cannot be changed at runtime and hence I suppose you have gone for the above approach.
will it be ok for you if you read the lines into an arraylist and convert it into the string, or do you "not" want to use collections at all and work only with the String .
If reading into the arraylist and then converting into the string  is ok with you, the API for collection classes provides you with the methods to return the content of the list in an  format.
If you do not want to use any collection classes like ArrayList, Vector etc what you could do is while reading from the file you can keep on concating the content of the readline to a string but seperated by a token. you can then use the tokenizer method to collect the differnt tokens into your String.
2. Unless you have some reasons for storing each line in a different  element, the better way to go about it is construct a single String for the whole file content.
thanks for saying it's a good start (was this a joke ?) actually the book i read was 600 pages, so i guess i'm a bit less "newbier" than in october.
i really find ArrayList useful and i think i'll keep on reading about collections, but for this time i just want to use a String...
i think your approach i quite cool, so this is how i tried to manage it
it's nice that we only have to read the file once. i also looked at the docs and it says String.concat "Concatenates the specified string to the end of this string" but it didn't work for me in the while loop above
Strings are immutable, so you can't actually change a string. If you read the full description of the concat method you will see
a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.
So you need to assign the return value of concat back to your original string.