aspose file tools
The moose likes Java in General and the fly likes how to read and show a byte array? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "how to read and show a byte array?" Watch "how to read and show a byte array?" New topic
Author

how to read and show a byte array?

Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
OK I have a file following:


I want to read it and show in the console, so i have made following.
Here is Message bean class to store data.


And here is main class to Test.


Here is result in the console of eclipse.

9:10:05 Long Message [B@19821f
9:13:58 Short Message [B@601bb1

I have got frmTime (9:10:05) and typeMsg (Long Message)but i dont know how to get the byte array (data) ?

Suggset me please!!!
Thanks and best regards.
[ November 07, 2007: Message edited by: Bear Bibeault ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
I'm guessing that strings like

030000002f130200000000000800450000800000000001119f7a0a000000e0e02f13c5dac5da006c0000000000000303000000000

are in hexadecimal (since I see digits and letters in the range a=f, but nothing else). Are you familiar with hexadecimal? Each character 0-9 or a-F corresponds to a 4 bits, so every two characters correspond to a single byte (8 bits). You will need to write some methods to read and write byte arrays in hexadecimal here. There are some existing methods that may help you, but you'll need to do some looping.

To read, try something like this:

You may find the Integer.parseInt() method useful here. Be sure to look a the overloaded versions of this method.

And to write:

If you're using JDK 5 or later, you can accomplish this simply using String.format() or System.out.format(). If not, you may need to do somethign like this:

If you can't use String.format(), this will require a little bit of simple math. Remember that every character has a numeric value - you can see a table here.


"I'm not back." - Bill Harding, Twister
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
Jim Yingst , thanks you very much,
But to be honest, i understand the aglothirm you suggest me but i am still not yet how to got it exactly in my code. I still code incorrect you can correct me by the way you can give me an example or a snippe code?
I am very sorry for my stupid.
thanks and best regards,
Hung
Quang Pham
Ranch Hand

Joined: Nov 29, 2005
Posts: 47
Hi Hung
It should work by changing your code like this:
while ((line = in.readLine()) != null) {String[] info = line.split(";");msg.setFrmTime(info[0]);msg.setTypeMsg(info[1]);
msg.setData(info[1].getBytes()); <-----------------
System.out.println(msg.toString());
}
But it seem like your data contains a bunch of PACK fields. To understand your data is another story.
Quang
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
Thanks Quang, i also edit the code following:

but it throw an exception follow:
java.lang.ArrayIndexOutOfBoundsException: 2
at test.TestMsg.readFileMsg(TestMsg.java:18)
at test.TestMsg.main(TestMsg.java:31)

I searched and think to slove this problem, but i still not yet modyfiy my code. With Jim Yingst's solution i still try to code but it still thrown exception and i can not ...
[ November 07, 2007: Message edited by: Tran Tuan Hung ]
Quang Pham
Ranch Hand

Joined: Nov 29, 2005
Posts: 47
Hi Hung
You can only store 1 frmTime, 1 typeMsg and 1 byte[] data per instance Message. I've shorten your data and modified the source code as follow and it works.
import java.io.BufferedReader;
import java.io.FileReader;
public class TestMessage {
private String filePath;
public void readFileMsg(Message msg) {
String line = null;
try {
String[] info = {"9:10:05","Long Message","030000002f130200000000000800450000800000000001119f7a0a000000e0e02f13c5dac5da006c0000000000000303000000000 00400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000"};
msg.setFrmTime(info[0]);
msg.setTypeMsg(info[1]);
msg.setData(info[2].getBytes());
System.out.println(msg.getFrmTime());
System.out.println(msg.getTypeMsg());
byte b [] = msg.getData();
for (int i=0;i< b.length;i++){
char c = (char)b[i];
System.out.print(c);
}
} catch (Exception e) { // TODO: handle exception//
e.printStackTrace();
}
}
public static void main(String[] args) {
Message msg = new Message();
TestMessage testMsg = new TestMessage();
testMsg.readFileMsg(msg);
}
}
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
I would like to get data from the text file, but BTW i also sloved my problem.
Thanks for helping me Jim Yingst and Quang.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: how to read and show a byte array?
 
Similar Threads
String, read and write file problem...
Sound recording
Updatin blob column in a table with file on my disk
How can I find out how many lines there are?