Skip to Content
author's profile photo Former Member
Former Member

Java File Download


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

I do it so:

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

			 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 );


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

		   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:



and so on..

Any Idea what is wrong?



Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Posted on 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.*


    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.