Read content of *.dat

Reply

Join Date: Dec 2004
Posts: 4,532
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 523
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Read content of *.dat

 
0
  #1
Dec 9th, 2005
I have small code to read datas from "dat" type of file. It's read fine just when last line is done it's kick-off with error message
Exception in thread "main" java.lang.NullPointerException at CW2.main<CW2.java:26>

import java.io.*;
import java.util.*;

class CW2
{
	public static void main( String[] args)
	{
		try
		{
		
			BufferedReader in = new BufferedReader( new FileReader("staff2005.dat"));
			String whole;
		
			while(true)
			{
				try
				{
					whole = in.readLine();
				}
				catch (EOFException eof)
				{
					System.out.println("End of File");
					break;
				}
				
				System.out.println( whole.trim());
			}
			in.close();
		}
		catch  (IOException e) 
		{
       System.out.println ( "IO Exception =: " + e );
      }

	}
}

Just learning, please don't shot me :o . Think there is a problem with my definition of try & catch. Highligted line 26 for you.

Can you please advice on this matter??
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,482
Reputation: jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all 
Solved Threads: 236
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Read content of *.dat

 
0
  #2
Dec 10th, 2005
When reading and writing binary files (which almost all files with a .dat extension are. There are thousands of different formats of course, dat is just short for data after all) don't use Readers and Writers.
Things like ByteArrayInputStream are your friends.
42
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,482
Reputation: jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all 
Solved Threads: 236
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Read content of *.dat

 
0
  #3
Dec 10th, 2005
Another option which I use (as I need read/write access at any point in a file with a fixed record format) is RandomAccessFile.

That yields a read method like this:
  1. public synchronized String[] readRecord(int recordId) throws
  2. RecordNotFoundException {
  3. String[] record = new String[6];
  4.  
  5. byte[] buffer = new byte[RECORD_LENGTH];
  6. int flag = 0;
  7. int bytesread = 0;
  8. Logger.getLogger("suncertify.server.bs").entering(this.getClass().
  9. getName(), "readRecord");
  10.  
  11. // try to move the pointer to the start of the requested record.
  12. // if this fails the record doesn't exist.
  13. try {
  14. datafile.seek(recordId * (RECORD_LENGTH + DELETE_FLAG_SIZE) +
  15. firstRecordOffset);
  16. } catch (IOException ex) {
  17. throw new RecordNotFoundException("Record " + recordId +
  18. " does not exist");
  19. }
  20. try {
  21. // read the flag to check whether the record is marked deleted
  22. flag = datafile.readUnsignedShort();
  23. if (flag == DELETE_FLAG) {
  24. throw new RecordNotFoundException("Record " + recordId +
  25. " has been deleted");
  26. }
  27. // read the record
  28. bytesread = datafile.read(buffer);
  29. } catch (IOException ex) {
  30. throw new DatabaseException("Error reading record " + recordId, ex);
  31. }
  32. StringBuilder buf = new StringBuilder(bytesread);
  33. for (int k = 0; k < bytesread; k++) {
  34. buf.append((char)buffer[k]);
  35. }
  36.  
  37. record[0] = buf.substring(0, 32);
  38. record[1] = buf.substring(32, 96);
  39. record[2] = buf.substring(96, 160);
  40. record[3] = (new Integer(buf.substring(160, 166).trim())).toString();
  41. record[4] = buf.substring(166, 174);
  42. record[5] = (new Integer(buf.substring(174).trim())).toString();
  43.  
  44. Logger.getLogger("suncertify.server.bs").finer(buf.toString());
  45. Logger.getLogger("suncertify.server.bs").exiting(this.getClass().
  46. getName(), "readRecord");
  47. return record;
  48. }
42
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,532
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 523
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Read content of *.dat

 
0
  #4
Dec 10th, 2005
Thanx for your advice. I did not get it working either way, but found that effective solution with use of Vector been sugested. My mistake that I did not seen link on the bottom of the page and trying to get my own solution to the problem. Always good programming exercise
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,482
Reputation: jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all 
Solved Threads: 236
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Read content of *.dat

 
0
  #5
Dec 12th, 2005
Preferably use List (especially ArrayList) instead of Vector to avoid the overhead caused by all the synchronization going on in Vector.
When and if you need that, you'll know it (and know the preferred alternatives in those cases as well).
42
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,532
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 523
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Read content of *.dat

 
0
  #6
Dec 12th, 2005
haha my teacher is oposite opinion, provided vector for reading from file. Than I used Tokenizer because I forgot there is split, but it worked fine. I learned somethig new :mrgreen: and gone be different from rest of people, so nobody can say I copied somebodies work.

Thank you for your advice. I may start hang around this forum for change of motion.

Cya around friend
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,482
Reputation: jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all 
Solved Threads: 236
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Read content of *.dat

 
0
  #7
Dec 13th, 2005
Your teacher probably never worked in the real world and learned Java from a very old book and never kept that knowledge up to date.
Vector predates List by several years, it's no longer recommended for use.

For performance I once did a small test.
Programmed a servlet that did several hundred inserts into a Vector (and then read them out again).
Timed that and repeated with an ArrayList.
The version using the ArrayList (and otherwise identical) was more than 10 times faster.
42
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 2013 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC