Results 1 to 7 of 7
Thread: Read a file bit by bit?
- 12-12-2009, 09:00 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 21
- Rep Power
- 0
Read a file bit by bit?
Hello. I have tried to google for this information, but people either instantly refer to the readByte() method in the various I/O packages or just say that it can't be done.
Anyways, I have ONE byte, in which each one of the 8 bits means a training for an employee in my data..
for instance...
The left most bit indicates orientation training
The 2nd bit from left indicates management training
The 3rd bit from left indicates technical training
The 4th bit from left indicates operations training
The 5th bit from left indicates administrative training
The 6th bit from left indicates quality control training
The 7th bit from left indicates sales training
The 8th bit from left indicates safety training
The problem is, I cannot figure out how to read in what trainings an employee has. If I had a readBit() method, I could easily use If Statements, etc. If the bit is switched on (1), then it means they have the training. This is the one stump I have left to overcome.
-
Why can't you read in the byte and then operating on the byte to get your information?
- 12-12-2009, 09:05 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 21
- Rep Power
- 0
-
You owe it to yourself to familiarize yourself with byte manipulation. You could have several byte constants as your masks and do a simple bitwise AND operations (using the & operator) to easily extract the different types of training an employee has performed. If this doesn't make sense, you may wish to google Java bitwise operations tutorials and see what comes up.
Much luck
-
I'm no expert in this (so corrections MOST welcome), but an enum could work nicely:
Java Code:public enum TrainingMasks { ORIENTATION(0x80), MANAGEMENT(0x40), TECHNICAL(0x20), OPERATIONS(0x10), ADMINISTRATION(0x08), QUALITY(0x04), SALES(0x02), SAFETY(0x01); private int mask; private TrainingMasks(int mask) { this.mask = mask; } public int getMask() { return mask; } public boolean isTrained(int test) { return (test & mask) != 0; } }
Java Code:public class TestMasks { public static void main(String[] args) { byte test = (byte)0xf2; for (TrainingMasks mask : TrainingMasks.values()) { if (mask.isTrained(test)) { System.out.println("Mask: " + mask + " is OK"); } } } }Last edited by Fubarable; 12-12-2009 at 09:39 PM.
- 12-12-2009, 11:21 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Or you could use an EnumSet:
Java Code:import java.util.Collections; import java.util.EnumSet; import java.util.Set; public class Training { public enum TrainingType { ORIENTATION, MANAGEMENT, TECHNICAL, OPERATIONS, ADMINISTRATION, QUALITY, SALES, SAFETY } private EnumSet<TrainingType> types = EnumSet.noneOf(TrainingType.class); public Training(byte b) { for(TrainingType t :TrainingType.values()) { if((b & 1 << t.ordinal()) != 0) { types.add(t); } } } public Set<TrainingType> getCanDo() { return Collections.unmodifiableSet(types); } public Set<TrainingType> getCantDo() { return EnumSet.complementOf(types); } public void add(TrainingType newT) { types.add(newT); } public boolean isTrained(TrainingType testT) { return types.contains(testT); } public static void main(String[] args) { Training test = new Training((byte)0xf2); System.out.printf("Can do: %s%n", test.getCanDo()); System.out.printf("Can't do: %s%n%n", test.getCantDo()); test.add(TrainingType.TECHNICAL); for(TrainingType type :TrainingType.values()) { System.out.printf("Can %s: %s%n", type, test.isTrained(type)); } } }Last edited by pbrockway2; 12-12-2009 at 11:24 PM.
- 12-12-2009, 11:37 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Sorry: put the TrainingType enum in its own file.
Also you can test whether a given level of training qualifies someone for a given task - since both tasks and people might have training associated with them:
Java Code:// in Training.java public boolean isTrained(Training testTask) { for(TrainingType t :testTask.types) { if(!types.contains(t)) { return false; } } return true; } // work like this: Training task = new Training((byte)0); task.add(TrainingType.TECHNICAL); task.add(TrainingType.OPERATIONS); System.out.printf("%n%s can do %s: %s%n", test, task.getCanDo(), test.isTrained(task));
Similar Threads
-
Read file from directory, update contents of the each file
By svpriyan in forum New To JavaReplies: 2Last Post: 05-11-2009, 10:07 AM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 10:28 AM -
file read
By makpandian in forum New To JavaReplies: 3Last Post: 12-11-2008, 04:02 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks