We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › copying a file
Page Index Toggle Pages: 1
copying a file (Read 1063 times)
copying a file
Feb 9th, 2010, 9:46pm
 
Ok, basic programming question here...

I'm just trying to open a .wav file and copy it byte by byte.  Why does this result in a gibberish file that is much bigger than my original?

Code:

// open a file and read its binary data
byte b[] = loadBytes("test.wav");

PrintWriter output;
output = createWriter("copy.wav");

//Print the whole thing to a file, a byte at a time
for (int i = 0; i < b.length; i++) {

   output.print(b[i]);

   }

//Close everything and quit
output.flush();
output.close();
exit();



Thanks......
Re: copying a file
Reply #1 - Feb 10th, 2010, 12:35am
 
Think about this...

In the ASCII character table (decimal) 72 is the letter 'H', and (decimal) 105 is the letter 'i'.

What happens when you do this?

Code:
print(72);
print(105);


You should get something "72105", not "Hi".

Does your "gibberish file" consist of a stream of numbers?

-spxl
Re: copying a file
Reply #2 - Feb 10th, 2010, 12:54am
 
See saveBytes() in the Processing reference for the 'opposite' of loadBytes().

saveStream() "is basically saveBytes(blah, loadBytes()), but done more efficiently (and with less confusing syntax)." If you just want to copy the file, this is probably what you want to use.

-spxl
Re: copying a file
Reply #3 - Feb 10th, 2010, 5:17am
 
Choose the right tools (which isn't easy in Java, as the numerous I/O classes are confusing!).
PrintWriter description excerpt: "Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams. "

[EDIT] I realize you probably only saw Reference's PrintWriter, but it also states text-output stream...
Page Index Toggle Pages: 1