This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes read text file into array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "read text file into array" Watch "read text file into array" New topic

read text file into array

haijun wang

Joined: Aug 18, 2000
Posts: 21
Dear Friends:
I have your help for read two text files into two different string arrays in order for me find the the words, which exist in one text file, also in other text file.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Which part do you need help with??
If you use a String array, you will probably end up with duplicate words in the arrays. That would give you the same results on the compare each time. Perhaps using a Set or a Sorted set would be better, and just discard words that you have already loaded.
You need to have 2 FileInputStreams (then you should run those through 2 DataInputStreams) then read the first file, one character at a time (using readChar()) appending the character onto a variable String - probably until you run into a space (that would be the end of the word). Then take the variable and load it into the first array at index 0, and clear the variable. Keep going until all of the words of the first file are loaded.
You could probably start reading the second file in the same manner until you find the first word, and stop and search the first array for that word right then. If you find a match, load the match into the second array.
Is this sort of what you need to do???

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: read text file into array
Similar Threads
B&S Parsing of Data file
DataInputStream Help needed while creating file
calling an external file
Error loading object from file in JasperReport Can any one Help...
read a text file