Results 1 to 6 of 6
  1. #1
    jasondeegan is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default [Solved] DataInputStream to file

    It's a rarity that I work in Java, so excuse the ignorance please. I've downloaded the StarPound suite of products (CEBPM tools) and am trying to write a file-conversion utility. I'm having trouble saving a URL to a file, as I don't know how. Here's the code:

    Java Code:
    try {
    	u = new URL( "http://test.wav" );
    	is = u.openStream();
    	dis = new DataInputStream(new BufferedInputStream(is));
    	while ((s = dis.readLine()) != null) {
    		//System.out.println(s);
    		// [COLOR="Red"]Instead of using System.out.println....I'd like to create a local file out of what I downloaded from the server.[/COLOR]
    	}
    } catch (MalformedURLException mue) {
    ...
    Thanks in advance!

    Jason:confused:
    Last edited by jasondeegan; 06-16-2010 at 02:30 PM. Reason: Solved.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Probably the simplest thing to do is to wrap a PrintWriter around a BufferedWriter, wrapped around a FileWriter. Your PrintWriter will have the same methods that System.out (which is a PrintStream) has, so you can easily adapt whatever sample code you are looking at.

    BufferedWriter (Java Platform SE 6)

    -Gary-

  3. #3
    jasondeegan is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    Appreciate the reply:

    Here's what I got...

    Java Code:
    import java.io.BufferedInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class MainClass {
    
      public static void main(String args[]) throws Exception {
    
        URL u = new URL("http://www.java2s.com/binary.dat");
        URLConnection uc = u.openConnection();
        String contentType = uc.getContentType();
        int contentLength = uc.getContentLength();
        if (contentType.startsWith("text/") || contentLength == -1) {
          throw new IOException("This is not a binary file.");
        }
        InputStream raw = uc.getInputStream();
        InputStream in = new BufferedInputStream(raw);
        byte[] data = new byte[contentLength];
        int bytesRead = 0;
        int offset = 0;
        while (offset < contentLength) {
          bytesRead = in.read(data, offset, data.length - offset);
          if (bytesRead == -1)
            break;
          offset += bytesRead;
        }
        in.close();
    
        if (offset != contentLength) {
          throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
        }
    
        String filename = u.getFile().substring(filename.lastIndexOf('/') + 1);
        FileOutputStream out = new FileOutputStream(filename);
        out.write(data);
        out.flush();
        out.close();
      }
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    I'd read and write at the same time, no need to hold the whole thing in memory and then write it out.

    And I don't think the BufferedInputStream is necessary, just use the original stream directly. I can't see any gain going through a BIS.

  5. #5
    jasondeegan is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the suggestion, Tolls. But your recommendation might as well have been in Greek. (I speak American (English, of course) ) Unfortunately I had to find code that I could augment/amend.

    I really appreciate everyone's attempts to help...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    This is the bit I mean about the buffered stream:
    Java Code:
        InputStream raw = uc.getInputStream();
        InputStream in = new BufferedInputStream(raw);
    ETA: That second line (the "in" InputStream) is completely unecessary.

Similar Threads

  1. Losing Packets/Bytes using DataInputStream and socket
    By ajordanneve in forum Networking
    Replies: 0
    Last Post: 03-24-2009, 05:22 AM
  2. Problem with DataInputStream
    By ZivonHunter in forum New To Java
    Replies: 1
    Last Post: 11-21-2008, 05:10 AM
  3. Replies: 0
    Last Post: 06-26-2008, 07:34 PM
  4. DataInputStream readLine()
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 10:44 PM
  5. Replies: 1
    Last Post: 08-07-2007, 07:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •