2

getting stressed over Java's many methods of I/O. BufferedReader/writer, Printwriter , ObjectInputStream and all that Jazz! Why can't Java be more simple?

here's my problem. Trying to write a client/server as exercise. here's my code for server which accept the connection and write to file.

String inputLine;
while (true){               
 connection = server.accept();          
 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
     BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
   while ((inputLine = in.readLine()) != null) {
        out.write(inputLine);
    }
   out.close();         
   in.close();          
   connection.close();          
}

At the client side: i am using this (sending a file over to server):

BufferedReader inPut =
      new BufferedReader(new InputStreamReader( new FileInputStream(fileName) ));
    PrintWriter output = new PrintWriter( client.getOutputStream(), true);

Although the file gets sent over, the file is corrupted. What is the most preferred method of I/O for sending over files through the network? Getting crazy over Java!

dorothy
  • 1,213
  • 5
  • 20
  • 35
  • Use Apache Commons library. It will make your life easy. – MD Sayem Ahmed Nov 20 '13 at 10:19
  • hi, thanks. wanted to use the standard library if possible – dorothy Nov 20 '13 at 10:20
  • It's complicated because verything (almost) is doable. You just need to understand what your desires are, and you'll understand which object to use. – Fabinout Nov 20 '13 at 10:20
  • 2
    All programming languages start simple, based on a single clear idea on how things should be done. Then people realise they can't do certain things easily so bits get bolted on, as more and more people use a language more and more things get bolted on; it gets more useful but also more noisy. Eventually people become frustrated with how disjointed the language is and say "we should write a new language based on a single clear concept. But then they find there are things they can't do easily so they bolt something on. Repeat forever – Richard Tingle Nov 20 '13 at 10:22
  • How exactly is the file corrupted? Is it a plain text file? – artbristol Nov 20 '13 at 10:33
  • hi, for example, sending over docx file or pdf file. Can't read with word or acrobat after that. – dorothy Nov 20 '13 at 10:36
  • docx and pdf files are not exactly just files, they have a complex data structure internally. If you would transfer a `.txt` file, then your code may still work if lucky. But see @artbristol's answer for more details on how to do it correctly. – skiwi Nov 20 '13 at 10:59
  • thanks all. manage to solve using BufferedInputstream/Outputstream. Sigh. – dorothy Nov 20 '13 at 11:20

1 Answers1

1

The problem is you're transferring a binary file line-by-line, as if it's text. This is bound to cause problems. Try a solution that doesn't use any Reader or Writer classes - you should operate directly on the byte streams.

artbristol
  • 32,010
  • 5
  • 70
  • 103