0

I'm trying to read a binary file (pdf, doc, zip) using InputStreamReader. I achieved that using FileInputStream, and saving the contents of file into a byte array. But i've been asked to do that using InputStreamReader. So when i'm trying to open and read a pdf file for example using

File file = new File (inputFileName); 
Reader in = new
InputStreamReader(new FileInputStream(file)); 
char fileContent[] = new char[(int)file.length()]; 
in.read(fileContent); in.close();

and then save this content to another pdf file using

File outfile = new File(outputFile);
Writer out = new OutputStreamWriter(new FileOutputStream(outfile));
out.write(fileContent);
out.close();

Everything goes fine (no exception or errors) but when i'm trying to open the new file, either it says it's corrupted or wrond encoding.

Any suggestion??

ps1 i specifically need this using InputStreamReader

ps2 it works fine when trying to read/write .txt files

yakamuki
  • 106
  • 1
  • 1
  • 8

2 Answers2

3

String, char, Reader, Writer are for text in java. This text is Unicode, and hence all scripts may be combined.

byte[], InputStream, OutputStream is for binary data. If they represent text, they must be associated with some encoding.

The bridge between text and binary data always involves a conversion.

In your case:

Reader in = new InputStreamReader(new FileInputStream(file), encoding);
Reader in = new InputStreamReader(new FileInputStream(file)); // Platform's encoding

The second version is non-portable, as other computers can have any encodings.

In your case, do not use an InputStreamReader for binary data. The conversion can only corrupt things.

Maybe they intended to mean: do not read all in a byte array. In that case use a BufferedInputStream to read small byte arrays (a buffer) repeatedly.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • i've tried the first way, but i can't find out what encoding does the binary file have. (eg pdf). I've tried all of the common encodings but nothing. The reason i'm trying to do that using Input StreamReader is because i've been asked to do so, and i don't know if it is possible to read the contents of a binary file properly with InputStreamReader. – yakamuki Oct 09 '14 at 14:35
  • Binary data like from a zip cannot be sensibly converted to text. You might use `StandardCharsets.ISO_8859_1`, ISO-8859-1 or Latin 1, a single-byte encoding. And convert back using a OutputStreamWriter. – Joop Eggen Oct 09 '14 at 15:35
1

Do not use reader/writer API. Use binary streams instead:

File inFile = new File("...");
File outFile = new File("...");
FileChannel in = new FileInputStream(inFile).getChannel();
FileChannel out = new FileOutputStream(outFile).getChannel();

in.transferTo(0, inFile.length(), out);
ursa
  • 4,404
  • 1
  • 24
  • 38
  • yeah i know, and the easiest way is to do it using bytes but i've been asked to do this with InputStreamReader, and i don't know if it's possible to read properly the content of a binary file this way – yakamuki Oct 09 '14 at 14:32
  • reader/writer API is designed to read text data. usually you should specify encoding of the text inside. this api is not applicable for binary data – ursa Oct 09 '14 at 14:33
  • so it's not possible with InputStreamReader right? actually i have to do a kind of encryption, adding to every single char a random number and then if i sub this same number from the encrypted file, it has to be again the same with the original one. i did that using bytes but i'm not able to do that with characters, when i cannot even read them properly – yakamuki Oct 09 '14 at 14:40
  • technically it is possible, if you enforce encoding in your program. e.g. you will always read/write files as text in "US-ASCII" encoding. but this is bad design. for math (encryption) manipulations it's better to interpret all files as binaries. – ursa Oct 09 '14 at 14:55