[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

newbie to Java IO tech

Posted on 2004-11-10
5
Medium Priority
?
214 Views
Last Modified: 2010-03-31
Hallo,

How do i convert a integer to a byte and add them to a bytearray  and then write to a outputstream.

similarly how do i convert a string to a byte  and add to a bytearray and then write to  a outputstream.

String s = "hallo";
byte[] a = String.getBytes(s);

i know this method but after converting to a byte array a
i need to add this to another byte array
how could i do it

finally combining integer and string  converted to a byte and adding them to a bytearray and later write to a outputstream

With regards,

Ashok.G
0
Comment
Question by:ashok3sep
5 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12542152
String value = String.valueOf(urInteger);
like:
String value = String.valueOf(12);

or like
int nVal = 15;
String statment = "The value is: " + String.valueOf(nVal);
byte[] bArray = statment.getBytes();
System.out.printlln(bArray0;
0
 
LVL 16

Expert Comment

by:gnoon
ID: 12542221
You can use java.io.DataOutputStream class for writing primative types (int, long, float, double, boolean etc.) to an outputstream.

DataOutputStream dataoutputstream = new DataoutputStream(outputstream);
dataoutputstream.write(1);
dataoutputstream.writeBoolean(true);
dataoutputstream.writeBytes("hello");
dataoutputstream.close();
0
 
LVL 16

Accepted Solution

by:
gnoon earned 100 total points
ID: 12542383
writing:

DataOutputStream dataoutputstream = new DataOutputStream(outputstream);
dataoutputstream.writeInt(1);
dataoutputstream.writeBoolean(true);
dataoutputstream.writeUTF("hello");
dataoutputstream.close();

reading:

DataInputStream datainputstream = new DataInputStream(inputstream);
int iV = datainputstream.readInt();
boolean bV = datainputstream.readBoolean();
String sV = datainputstream.readUTF();
datainputstream.close();
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 12542729
>> i need to add this to another byte array

Add to another byte-array? Do you mean you want to add these values with the values in another byte[] or do you want to append to another byte[] ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12542816
>>adding them to a bytearray and later write to a outputstream

You may be OK simply writing the first array to the stream, followed by the second one
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question