hi,

which one of these two would be better to design a file transfer protocol FileInputStream or FileReader? i know FileInputStream is meant to be for image type of files........

thanks

Dani AI

Generated

Short answer for : use byte-oriented I/O for a file-transfer protocol. Readers/Writer classes are for character data and perform charset decoding; using them on binary files will corrupt the bytes. was pointing in the right direction — here’s a compact, practical checklist and example to make a reliable transfer.

Use bytes, not chars

  • Read and write raw bytes (InputStream/OutputStream or java.nio Channels). Wrap with buffering for throughput (BufferedInputStream/BufferedOutputStream). Use try-with-resources so streams are always closed.
  • Example sender loop (copy file to a socket OutputStream):
try (InputStream in = new BufferedInputStream(new FileInputStream(srcPath));
     OutputStream out = new BufferedOutputStream(socket.getOutputStream())) {
    byte[] buf = new byte[8192];
    int r;
    while ((r = in.read(buf)) != -1) out.write(buf, 0, r);
    out.flush();
}

Protocol and robustness checklist

  • Send metadata first: filename, length, content-type, and a checksum (use SHA-256) so receiver can verify integrity.
  • Frame messages explicitly (length-prefixed chunks or explicit start/finish markers) so the receiver knows where file data begins/ends.
  • For large files support chunking and resume (send byte ranges or numbered chunks). On the receiver write to a temporary file (e.g., name+".part"), verify size/hash, then atomically rename.
  • Sanitize incoming filenames to prevent directory traversal, enforce quotas, and set timeouts to avoid resource exhaustion.

Performance & tooling

  • For simple cases Buffered streams + sensible buffer (8–64 KB) are fine. For large transfers or local copies consider Files.copy or FileChannel.transferTo/transferFrom for better throughput.
  • Always use TLS for transfers over untrusted networks and validate checksums after transfer.

Bottom line: FileInputStream (or NIO channels) + buffering + explicit framing and integrity checks is the correct approach. Avoid FileReader for transfers unless you intentionally transfer only text and agree on the charset.

Recommended Answers

All 3 Replies

Readers and Writers work only on line based character data, so plain text files.
For anything else, you MUST use Streams.

hi,

thanks for the reply.

When designing a java application(not web based), do you have to do class diagrams and domain models or just one would do. because as far as i know class diagrams should be done for each use case and domain diagram describes the overall system......is that correct?

thanks

Don't stare blindly at what diagrams to use. Use them when you see the need.

You're falling into the trap UML sets for the unwary (which sadly includes many "architects" and "managers") which is to think it's a goal in itself, maybe even more important than the final code.
It's not, it's merely a tool to help you visualise and communicate your design choices more clearly than the written word alone can do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.