Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7824641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:40:01+00:00 2026-06-02T08:40:01+00:00

What I need to do is send multiple files using DataStreams. I’m doing this

  • 0

What I need to do is send multiple files using DataStreams. I’m doing this by sending the name of the file, and then the file’s bytes. I need to send an undetermined number of files though. Here is the DataOutputStream code.

            out.writeUTF(path);
            out.write(Files.readAllBytes(file.toPath()));

It does that for each file that needs to be sent. But I don’t know how to read it correctly with DataInputStream. This is what I have so far.

    while (in.available() != 0) {
        String path = in.readUTF();
        byte bytes = in.readByte();
    }

Obviously it wouldn’t work, since it is only reading one byte. But I don’t know how to make it read all of the bytes. Since there are several files being sent, available() would only equal 0 when the end of all the files are read, I think. Any help is greatly appreciated.

Something I completely forgot to mention, I want to be able to send a large file without running out of memory, and I don’t think this would work. I think I would need to use a buffer, but I don’t know what class supports that with files.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:40:04+00:00Added an answer on June 2, 2026 at 8:40 am

    Anytime you send variable length messages you need some way to mark the beginning and end of each method.

     List<File> files = someListOfFilesYouWantToSend;
     out.writeInt(files.size());
     for(File file : files){
       out.writeUTF(path);
       out.writeLong(file.getTotalSpace());
       out.write(Files.readAllBytes(file.toPath()));
     }
    

    Then to read it you would do something like this

    int filesToRead = in.readInt();    
    for(int i = 0; i < filesToRead; i++){
      String path = in.readUTF();
      long bytesToRead = in.readLong();      
      FileOutputSteam fos = new FileOutputStream(path);
    
      byte[] buffer = new byte[1024];
      while(bytesToRead > 0){
        bytesRead = in.read(buffer,0,bytesToRead > buffer.length ? buffer.length : bytesToRead);
        bytesToRead -= bytesRead;
        fos.write(buffer);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a distributed application consisting of multiple clients that send files
I'm trying to send multiple images over a socket using java but I need
I need send certain attributes(say, human readable user name) from server to client after
I've a custom form (created with form API) that need send an uploaded file
I need to send this XML <?xml version=1.0 encoding=UTF-8?> <gate> <country>NO</country> <accessNumber>1900</accessNumber> <senderNumber>1900</senderNumber> <targetNumber>4792267523</targetNumber>
I need to send floating point numbers using a UDP connection to a Qt
I need to send data to a hardware device over serial port. I'm using
I need to send byte[] array using WCF services and I care about message
I need to send a file to MobileMe via Cocoa. I stumbled across a
I need to split a bizTalk message in the send pipeline. This is easy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.