File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes File I/O - How to do? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "File I/O - How to do?" Watch "File I/O - How to do?" New topic
Author

File I/O - How to do?

Chris Mathews
Ranch Hand

Joined: Jan 08, 2001
Posts: 46
I am trying to read from a file and then write to a file.
The input file looks like following :
07200608240190200105,WA07MA44,K
07200608340190200105,WA07MA37,K
07200608240190200105,WA07MA34_P2, K
07200608240190200105,WA07MA34_P1,K
07200608440190200105,WA07MA32,K
07200608340190200105,WA07MA31,K
07200608640190200105,WA07MA30_P2,K
07200608240190200105,WA07MA30_P2,K
07200608540190200105,WA07MA28_P2,K
07200608840190200105,WA07MA28_P1,K
07200608240190200105,WA07MA20_P2,K
07200608240190200105,WA07MA20_P2,K
07200608340190200105,WA07MA18,K
The Output should be :
The total Number of records in the input file : < the total number of records > -- In our case it should be 13
The number of unique values in the first column : This case it should be 6
The number of unique values in the secind column : This case it should be 11
I am cluseless about how to achieve this.Anybody please help me with the idea and/or code.
Thanks,
Chris.

Scott Palmer
Ranch Hand

Joined: Jul 11, 2001
Posts: 31
I would start with a StreamTokenizer class (java.io.StreamTokenizer)
http://developer.java.sun.com/developer/TechTips/1998/tt0722.html
Configure it to seperate tokens with commas, then read each record on field at a time and construct an object that holds the record.
Store each record object in a collection class (hint: something that implements 'SortedSet') write a Comparator method for the record type that will sort by the first field. (Just delegate to the String comparator for the first field.)
Once you have the records sorted, iterate through the sorted list to count when the first field changes. That will give you the number of unique first fields.
Scott Palmer
Ranch Hand

Joined: Jul 11, 2001
Posts: 31
I forgot to mention that to get the unique entries for the second column you would just sort the items using a different comparator that used the second column instead of the first. You could easily put each record into two collections, each one sorted a different way.
Chris Mathews
Ranch Hand

Joined: Jan 08, 2001
Posts: 46
Scott,
Can you provide me with some code ?I am getting the idea but still not sure how will I achieve this?
Thanks.
Detlev Beutner
Ranch Hand

Joined: Jul 13, 2001
Posts: 76
Scott: Sorry, but I disagree. First, the StreamTokenizer does not make too much sense if (a) you know the structure of the input very well and (b) this structure is very easy. Both is given in this case. Second, a SortedSet would mean, as you stated yourself, that you have to count the unique values manually (after having them sorted anyhow!). This will take about the same time as you let the JVM do this job, but it is too much work implementing it.
Chris: I think a short and nice solution is this (you would have to add some stuff about the output):

Hope it helps
Detlev
[This message has been edited by Detlev Beutner (edited July 16, 2001).]
Scott Palmer
Ranch Hand

Joined: Jul 11, 2001
Posts: 31
Yes.. my solution was perhaps a bit complicated, almost on-purpose since this looks suspiciously like a homework assignment.
But also because I was thinking for some reason that he also needed to know the number of duplicates for each unique value - I wish you could see the thread you are replying to while you write the reply!
Hash set is the way to go to be more efficient .. but storing all those records in a HashSet is much less usefull if you actually needed those duplicate records for something (which you don't given the problem as stated - but part 'B' of that homework assignment might need it ).
The stream tokenizer - well that was overkill for this problem for sure.. and again I was thinking too much. The stream tokenizer is designed for parsing text files which is what we have here, so it just popped into my head as something to try.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: File I/O - How to do?
 
Similar Threads
File I/O - How to do?
OutofMemoryError
File I/O - How to play with files ?
File I/O - How to do?
File I/O - How to do?