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 Little help with reading files 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 "Little help with reading files" Watch "Little help with reading files" New topic
Author

Little help with reading files

bert ngo
Greenhorn

Joined: Nov 20, 2008
Posts: 6
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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?


[Jess in Action][AskingGoodQuestions]
bert ngo
Greenhorn

Joined: Nov 20, 2008
Posts: 6
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

Joined: Nov 10, 2008
Posts: 106

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
Software Developer for SGI Sistemas, lukas1596@gmail.com
Lucas Franceschi
Ranch Hand

Joined: Nov 10, 2008
Posts: 106

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Nov 10, 2008
Posts: 106

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

Joined: Nov 20, 2008
Posts: 6

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

Joined: Oct 13, 2005
Posts: 38045
    
  22
"Because" please, not "cuz." Please have a look at this FAQ
bert ngo
Greenhorn

Joined: Nov 20, 2008
Posts: 6
I apologize I will remember for next time to use real words.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Apologies accepted
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Little help with reading files
 
Similar Threads
How to store image, audio, video & text files POSTed to JSP page?
URGENT help needed, URLyBird
dynamic jasper report
How to read the complete contents of a file in a String?!
Reading data from files