Skip to Content
0
Former Member
Sep 28, 2005 at 12:17 PM

Java File Download

36 Views

Hi,

I want download a .dat file from a website in my java class.

I do it so:

 FileWriter fw = null;
		   SimpleDateFormat fmt = new SimpleDateFormat();
		   try
		   {

			 String filename_t = ("/appl/shared/file.dat");

			 fmt.applyPattern( " adress/file'ddMMyy'.dat' " );
			 Calendar cal = new GregorianCalendar();

			 URL url = new URL( fmt.format(cal.getTime()));
			 Reader is = new InputStreamReader( url.openStream() );
			 BufferedReader in = new BufferedReader( is );

			 FileOutputStream file = new FileOutputStream( filename_t );
			 ObjectOutputStream o = new ObjectOutputStream( file );

			 StringWriter buffer = new StringWriter();
			 PrintWriter out = new PrintWriter( buffer );

			 for ( String s; ( s = in.readLine() ) != null; )

			   out.println( s );

			 in.close();

			 String result = buffer.toString(); 
             
			 o.writeObject( result );

			 o.close();
		   }
		   catch ( MalformedURLException e ) {
			   System.out.println( "MalformedURLException: " + e );
		   }
		   catch ( IOException e ) {
			   System.out.println( "IOException: " + e );
		   }    
       
    }


Everything works, but in editor the downloaded files looks like:

ŸnԀʹŸnԀʹŸnԀʹŸnԀʹŸnԀʹŸnԀʹŸnԀʹŸnԀʹ.

ŸnԀʹŸnԀʹŸnԀʹŸnԀʹŸnԀʹ

and so on..

Any Idea what is wrong?

Regards,

Robin