File Transfer

Hey, I am trying to send a file over a socket, so far I have split the file into byte[] arrays, and sent them over the PrintWriter (as it had a method for sending byte arrays), my question is, how do I receive them?

I was originally writing and reading each byte individually on separate lines with:

[code]

String received = in.readLine();

while (!received.startsWith("T")) { //the first string sent after the bytes of the file begins with a T.

testFile.write(Integer.valueOf(received).intValue()); //this byte is written to the file.

received = in.readLine();

}[/code

Where 'in' is a BufferedReader.

I know there is a BufferedReader method for read(char[] cbuf, int off, int len) , but as far as I can tell this receives separate characters, and then places them into a byte array, how do I just receive a byte array straight from the socket?

Thank-you in advance.

[934 byte] By [abu5ea] at [2007-11-26 19:22:40]
# 1

Nevermind that question, I realised the PrintWriter has a method for sending a char[] not a byte[], and so the code was just sending byte[].toString().

My revised question is this: if byte[].toString() is how it is sent, why can't I receive it with byte received[] = (byte[])in.read();

?

If this isn't the way to do it, how can I convert back from a string to a byte array?

Thank-you

abu5ea at 2007-7-9 21:43:32 > top of Java-index,Java Essentials,Java Programming...
# 2

> My revised question is this: if byte[].toString() is how it is sent, why can't I

> receive it with

>

> byte received[] = (byte[])in.read();

because in.read(); returns a single character

you could use DataInputStream with the method read(byte[] b) and construct the File with FileOutputStream with the method write(byte[] b)

Ruly-o_Oa at 2007-7-9 21:43:32 > top of Java-index,Java Essentials,Java Programming...