read a file 2 times

Hi all
I have requirement where in I need to read a file two times start to end.To be elaborate I have to read through the file start to end and when I encounter the EOF(end of file) I need to read it agaoin from start to end.There is a prticular reason why Iam doing this.Can anyone please let me know how I can read 2 times.Thanks
dmoreAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
objectsConnect With a Mentor Commented:
repeat what you do to read it once :)
making sure to close it between reads :)
0
 
CEHJCommented:
mark() and reset() it
0
 
dmoreAuthor Commented:
sorry I will be bit more clear with my requirement  
I have to read a file in two passes, the first pass builds a HashMap of all records in a file. the second pass copies records  to a  file for transactions that are in  hashmap bulit in the first pass.Any ideas or sample code please

Thanks
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJCommented:
You could just iterate the Map forthe second
0
 
dmoreAuthor Commented:
Hi CEHJ
Thanks
My file has content like
transaction id|identifier|message now depending on the identifier I add the transaction id to the hashmap in my first pass through the file.
in the second pass I check if the transaction id that is in the file is also in the hashmap , if yes, I copy the message to another file.Can you please supply me with any sample code for this.

Thanks in advance
0
 
CEHJCommented:
>>I add the transaction id to the hashmap in my first pass through the file.

As a key or value? If the former, it can only go in once
0
 
dmoreAuthor Commented:
so do you mean I have to read the file twice separately to acheive two separate operations
0
 
objectsCommented:
> so do you mean I have to read the file twice separately to acheive two separate operations

thats correct
0
 
CEHJCommented:
Not necessarily - firstly i'm trying to establish what you're doing, hence my last question
0
 
CEHJCommented:
>>so do you mean I have to read the file twice separately to acheive two separate operations

Probably not. Why don't you just save the message along with the id, then you can just write the file based on the Map contents?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> depending on the identifier I add the transaction id to the hashmap in my first pass

What is the condition on basis of which you do it?

>> in the second pass I check if the transaction id that is in the file is also in the hashmap

So anyway what you need to do is copy the message for those transaction IDs which are in the hash-map (which satisfy the first condition) - you can directly take the message out as well. Make a wrapper/ value object class like:

class Wrapper
{
  private String identifier ;
  private String transactionID ;
  private String message ;
  public void setIdentifier ( String identifier )
  {
    this.idenfitier = identifier ;
  }
  public String getIdentifier ()
  {
    return identifier ;
  }
  // same way, add getters/ setters for the rest
}

When you read for the first time, you can store an object of the above in the hash-map (with identifier as the key, the object as the value). Then directly read from the hash-map and write the messages to another file.
0
 
CEHJCommented:
>>you can store an object of the above in the hash-map

If you do that, you'll need to override equals and hashCode or it won't work properly
0
 
dmoreAuthor Commented:
Hi All
I have another query..I have built a hashmap now I have following queries.Any ideas please

1.I have to first set the transaction pointer to the first entry in the hashmap.
2.I need to get the next value in hashmap each time I call a method which just returns the value in a hashmap.
0
 
CEHJCommented:
>>1.I have to first set the transaction pointer to the first entry in the hashmap.

HashMap is inherently unordered. If you need ordering, use LinkedHashMap

2. See above (although it sounds like a List<Transaction> may be better)
0
 
dmoreAuthor Commented:
Well , I don't need ordering just set the pointer to first element
and whenever I call the method it should return the next value..any suggestions?
0
 
CEHJCommented:
>>Well , I don't need ordering just set the pointer to first element

How do you know what the first one is if it's unordered?
0
 
CEHJCommented:
If you turn it back into List<Transaction> from a Map then you can call iterator() on it, which does what you just described
0
 
dmoreAuthor Commented:
I do not need to know what ever firstelement hashmap holds I just return it..
0
 
CEHJCommented:
There isn't a 'first' in any meaningful way. From your point of view, what comes out first is entirely random
0
 
CEHJCommented:
... *but*, as i said earlier, if you use a LinkedHashMap, an Iterator over the entries will return first the first entry you inserted, provided it didn't get superseded
0
 
dmoreAuthor Commented:
Hi CEHJ
Can I loop though the hashmap say if I have some pointer which is an integer ?
0
 
CEHJCommented:
Map is for lookups. If you have a key (this is what i guess you mean by 'pointer') then you use the key to do a lookup
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> If you do that, you'll need to override equals and hashCode or it won't work properly

Why? It will be the value, not the key. The key is still a String and we don't need to override equals or hashCode () while using a String key for a hash-map.

>> Can I loop though the hashmap say if I have some pointer which is an integer ?

You can iterate through it:

Object[] array = hashMap.values ().toArray () ;

for ( int i = 0 ; i < array.length ; i ++ )
  System.out.println ( ( ( Wrapper ) array[i] ).getMessage () ) ;
0
 
CEHJCommented:
>>Why?

Because 'Wrapper' doesn't implement equals, it will allow duplicates to occur, thus breaking the Map. hashCode should be implemented in case it's needed too
0
 
Mayank SAssociate Director - Product EngineeringCommented:
CEHJ, the key will be the identifier (String) and I guess identifier is unique in this case (from the file).

>> it will allow duplicates to occur thus breaking the Map

The Map values can be duplicates, but not the keys. In this case however, both will be unique (assuming that identifier is unique).
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Key - String (identifier)
Value - Object of Wrapper (so it need not implement equals and hashCode)
0
 
CEHJCommented:
OK - i've got it now. A Map is in fact redundant and i was thinking of a Set, for which of course you'd have to override hashCode and equals. If a TreeSet is used it could be stored in id order
0
 
dmoreAuthor Commented:
Hello all,
I have a new problem now, I have to read the last line of a file whic Ima currenlty doing using some inputs suggested at http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21805554.html.
but the problem is that my file has a line that could extend into more than one line but the entire thing needs to be considered as a single line.

for eg: my file has lines of format  
|xxx|yyy|zzz|fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff         the fff.... part can extend into more than one line but with my program the put put comes as  'ffffffffffffffff ' and not |xxx|yyy|zzz|fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
can anyone please help
0
All Courses

From novice to tech pro — start learning today.