How to download a file from a url

D

De Wayne Dantzler

I'm new to java and I'm trying to download a file from a url. I've tried
the following scheme given below, but the contents of the file is
garbage. One file is binary and the other is xml. I always get an
EOFException even though I check for a -1 return value. Any clues?

public void writeBinaryData2File( String fileOut ) throws IOException
{
BufferedInputStream bis = new
BufferedInputStream(_MyUrl.openStream());
DataInputStream dataIn = new DataInputStream(bis);
FileOutputStream fos = new FileOutputStream(fileOut);
DataOutputStream dataOut = new DataOutputStream( fos );

byte bdata;
while( (bdata = dataIn.readByte()) != -1 )
{
dataOut.writeByte( bdata );
}
}

public void writeXml2File( String fileOut ) throws IOException,
EOFException
{
BufferedInputStream bis = new
BufferedInputStream(_MyUrl.openStream());
DataInputStream dataIn = new DataInputStream(bis);
FileWriter fw = new FileWriter(fileOut);

int cdata;
try
{
while((cdata = dataIn.readInt()) != -1 )
{
fw.write( cdata );
}
}catch( EOFException e) { fw.close(); e.printStackTrace(); }
}
 
R

Roedy Green

{
BufferedInputStream bis = new
BufferedInputStream(_MyUrl.openStream());
DataInputStream dataIn = new DataInputStream(bis);
FileOutputStream fos = new FileOutputStream(fileOut);
DataOutputStream dataOut = new DataOutputStream( fos );

byte bdata;
while( (bdata = dataIn.readByte()) != -1 )
{
dataOut.writeByte( bdata );

If you are just going to write raw bytes, you don't need a
DataOutputStream. A plain FileOutputStream would suffice,
ditto for input.

See also the FileTransfer classes to do this faster, not just a byte
at a time. http://mindprod.com/products.html#FILETRANSFER
 
N

Neomorph

I'm new to java and I'm trying to download a file from a url. I've tried
the following scheme given below, but the contents of the file is
garbage. One file is binary and the other is xml. I always get an
EOFException even though I check for a -1 return value. Any clues?

public void writeBinaryData2File( String fileOut ) throws IOException
{
BufferedInputStream bis = new
BufferedInputStream(_MyUrl.openStream());
DataInputStream dataIn = new DataInputStream(bis);
FileOutputStream fos = new FileOutputStream(fileOut);
DataOutputStream dataOut = new DataOutputStream( fos );

byte bdata;
while( (bdata = dataIn.readByte()) != -1 )
{
dataOut.writeByte( bdata );
}
}

Why are you using a DataInputStream ?
You know that a Byte is not the same as a byte, right ?
One is a (final) class, the other a primitive variable type.

You do not need the Data(input/output)Streams here.
And don't forget to close the streams when you're done !
public void writeXml2File( String fileOut ) throws IOException,
EOFException
{
BufferedInputStream bis = new
BufferedInputStream(_MyUrl.openStream());
DataInputStream dataIn = new DataInputStream(bis);
FileWriter fw = new FileWriter(fileOut);

int cdata;
try
{
while((cdata = dataIn.readInt()) != -1 )
{
fw.write( cdata );
}
}catch( EOFException e) { fw.close(); e.printStackTrace(); }
}

Same here: are you trying to read from a file that contains Integer class
instances, or text ?

Cheers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top