• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Little help with reading files

 
bert ngo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I just started my java data structure class this semester and part of my homework is to make a department report. The part where i'm having hard time thinking about is how to read a binary file hoping anyone could give a steer in the right direction to how to read binary files and then inputting that data into a department report. I know how to read the file that makes up the departments which i have shown below. I was thinking if the employee is a binary file would i have to use a DataInputStream?
thanks for the help

A,Administration,302
E,Engineering,186
C,Construction,240
S,Sales,239
M,Marketing,250
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean exactly by "binary file?" What you've shown looks like text. If they've actually given you a binary file to read, they would have to specify the format exactly; have they done that?
 
bert ngo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im sry let me clarify wat i mean. first i cannot attach the file i need to read because it is a .dat file. So my professor has given me a file called Employee.dat and i need to read it. I need to be able to read the file so the following is defined:

EmployeeID - 3 bytes ASCII
Employee/Contractor Indicator - 1 byte ASCII 'E' for employee, 'C' for contractor
Department Code - 1 byte ASCII
First Name - 20 bytes ASCII blank padded
Last Name - 15 bytes ASCII blank padded
Salary - float single precision
Hire Date - 1 byte binary identifies the month, 1 byte binary identifies the day, 2 bytes binary identifies the year
Vacation Days - 2 bytes signed binary
Training - 1 byte

hope i cleared this up. O and the text i showed in my first post is the departments i need to have Employee.dat inputted into it. The numbers after each department will be its corresponding department code
A,Administration,302
E,Engineering,186
C,Construction,240
S,Sales,239
M,Marketing,250
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, actually you would need to have a FileReader and Buffered Reader (take a look here for this classes.)

As I said, try lookin on documentation or even in javaranch for this classes, you may find something that helps you more.

Then, to read the file in a linear way [1st line -> 2nd line -> 3rd line .....], you'll need to have a loop, that will loop until the buffered reader can't find a "next line"


hope i've helped


Regards!
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i'm also sure that if your professor asked you to solve this, is because solving it is part of your education. asking in forums will get a respone and will for sure make you learn how to do it, but you lose the greater learning that you have when you need to solve something on your own, and I think that this greater learning is that your professor wants you to have.

anyway, good luck.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucas Franceschi wrote:well, actually you would need to have a FileReader and Buffered Reader


Please note that this is appropriate only for a text file, and not for a binary file! The "Reader" classes are only appropriate for pure text files.

Your original suggestion of DataInputStream was absolutely correct; you'll want to use a FileInputStream and a DataInputStream together:

DataInputStream dis = new DataInputStream(new FileInputStream("yourfile.dat"));
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:
Please note that this is appropriate only for a text file, and not for a binary file! The "Reader" classes are only appropriate for pure text files.


sorry, didn't noted that.
 
bert ngo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

DataInputStream dis = new DataInputStream(new FileInputStream("yourfile.dat"))


Thanks a lot i had a hard time thinking about how to reads this file cuz i didnt do alot with files in my previous course. I think i can handle the rest
thanks again!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Because" please, not "cuz." Please have a look at this FAQ
 
bert ngo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize I will remember for next time to use real words.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies accepted
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic