Skip to Content
author's profile photo
Former Member

Java File Download

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

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • Oct 02, 2005 at 09:31 AM

    a) Do you know what the content of the .dat file should be / look like?

    b) Do you know in which ccsid (char code set id) it was written. May be you need to translate using java.nio.*

    Enjoy

    Add comment
    10|10000 characters needed characters exceeded