Click to See Complete Forum and Search --> : Copying A Binary file


lazy_cod3R
07-20-2001, 08:49 PM
Hi All,
I need some help on copying binary files. For example i have a wave file and i want to write a peice of java code that will read this wave file and write it else where and not correupt the wave file but still allow it to be playable.
At current time i have tried to read it byte by byte and write it again but this doesnt work, then i tried to read it as a string , line by line and it doesnt write the whole wave file properly.

What im trying to do is make a voice chat program. I need to send the wave file to a remote object on another machine. The only method i can think of doing this right now is to read the wav file byte by byte or line by line, store each line or byte in an array list and send that arraylist through the network, i can send the data properly but when i traverse the arraylist and try to write the contents of it back to a file to play it, the file is total corrupted.
that is why i need help on how to read the wav file properly so it isnt corrupted when i send it through the network, what would be the general method of doing this ...

Any help is appreciated
Thanx

jemfinch
07-21-2001, 01:30 AM
I don't do java, but in any programming language I use, I'd open the file (in binary mode, if running under windows), read the contents to a string, and do what I want with it from there.

I can't see why it would be any different in Java. Perhaps you can elaborate on your problems you've been experiencing with the above method.

Jeremy

lazy_cod3R
07-21-2001, 04:28 AM
when i read the contents out of the file, its a wav file, i want to store each line into an array or arraylist and send this datastructure through the network, on the other machine when i traverse the array and try to write the contents of the wave file to disk and try to play it, it's no longer playable, it doesnt even pass as a wave file anymore , because when i click it to play the file it says , not valid wave file.

In java how would you open the file in binary mode ? what class would you use ??

thanx for any help

jemfinch
07-21-2001, 02:41 PM
The idea of storing a binary file "by lines" isn't valid. Binary files do contain newline characters, but they don't mean the file has "lines". Just send chunks of it at a time, maybe 1024 bytes at a time or so.

Jeremy

Stuka
07-22-2001, 01:35 AM
One possible issue - since Java has Unicode support, if you're trying to do the bytes as strings, you're probably messing them up. If this were in C/C++, I'd suggest a low level file reading effort using unsigned chars or the like for your bytes. My "Java in a Nutshell" book mentions that FileInputStream reads in bytes from a file (and will read them into a byte[] array), and that FileOutputStream writes out bytes to a file.
Are you using these classes, or another?

[ 22 July 2001: Message edited by: Stuka ]

Stuka
07-22-2001, 06:46 PM
OK - did this in Windows, and hardcoded the filenames, but it works 100%:
import java.io.*;

public class WaveCopier{

public static void main(String[] args){
try{
FileInputStream in = new FileInputStream("f:\\webpag~1\\sounds\\happy.wav");
FileOutputStream out = new FileOutputStream("c:\\test.wav");
byte[] buffer = new byte[1024];
int iter = 0;
while(in.read(buffer, iter * 1024, 1024) != -1){
out.write(buffer, iter * 1024, 1024);
System.out.print("*");
}
}
catch(IOException e){
System.out.println("I/O Error");
}
}
Maybe not pretty, but it works! :D