追加二进制文件的内容

huangapple go评论102阅读模式
英文:

Appending content of binary files

问题

我想将一个文件的内容追加到另一个已存在的文件中。我使用了带有追加参数的FileOutputStream以及SequenceInputStream进行普通文件复制。这两种方法对于txt文件都有效,但对于二进制文件如pdf和excel则不起作用。

如果我尝试合并两个pdf文件,总是会覆盖结果文件中的第二个输入流。是否有其他方法可以实现二进制文件的相同操作?

以下是我的代码。

  1. package org.saurav.simpletests.io;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.SequenceInputStream;
  9. public class ConcatStreams {
  10. public static void main(String a[]) {
  11. ConcatStreams concatStreams = new ConcatStreams();
  12. try {
  13. InputStream input1 = new FileInputStream("<path to first binary file>");
  14. InputStream input2 = new FileInputStream("<path to second binary file>");
  15. OutputStream output = new FileOutputStream("<path to first binary file> ", true);
  16. //concatStreams.mergeUsingSequenctInputStream(input1,input2); // uncomment it to run the code with sequenceInputStream
  17. concatStreams.mergeUsingFileOutputAppend(output, input2);
  18. } catch (FileNotFoundException e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. }
  22. }
  23. private void mergeUsingFileOutputAppend(OutputStream outStream, InputStream input) {
  24. try {
  25. byte[] buffer = new byte[256];
  26. int data;
  27. data = input.read(buffer);
  28. while (data != -1) {
  29. String str = new String(buffer, "UTF-8");
  30. //System.out.println(str);
  31. outStream.write(buffer);
  32. data = input.read(buffer);
  33. }
  34. //output.write(data);
  35. } catch (IOException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. } finally {
  39. try {
  40. outStream.close();
  41. } catch (IOException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. private void mergeUsingSequenctInputStream(InputStream input1, InputStream input2) {
  48. SequenceInputStream sequenceInputStream =
  49. new SequenceInputStream(input1, input2);
  50. FileOutputStream fos = null;
  51. try {
  52. fos = new FileOutputStream("<path to first binary file>");
  53. byte[] buffer = new byte[256];
  54. int data;
  55. data = sequenceInputStream.read(buffer);
  56. while (data != -1) {
  57. String str = new String(buffer, "UTF-8");
  58. //System.out.println(str);
  59. fos.write(buffer);
  60. data = sequenceInputStream.read(buffer);
  61. }
  62. } catch (IOException e) {
  63. // TODO Auto-generated catch block
  64. e.printStackTrace();
  65. } finally {
  66. try {
  67. fos.close();
  68. } catch (IOException e) {
  69. // TODO Auto-generated catch block
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74. }

最好的问候,
Saurav

英文:

I want to append the content of one file to another existing file with content. I used plain file copy with FileOutputStream with append parameter and also with SequenceInputStream. Both are working for txt files but not for binary files like pdf and excel.

If i try to merge two pdf files always the second input stream is overwritten in the resultant file. Is there any other way i can achieve the same for binary files ?

Below is my code.

  1. package org.saurav.simpletests.io;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.SequenceInputStream;
  9. public class ConcatStreams {
  10. public static void main (String a[]) {
  11. ConcatStreams concatStreams = new ConcatStreams();
  12. try {
  13. InputStream input1 = new FileInputStream(&quot;&lt;path to first binary file&gt;&quot;);
  14. InputStream input2 = new FileInputStream(&quot;&lt;path to second binary file&gt;&quot;);
  15. OutputStream output = new FileOutputStream(&quot;&lt;path to first binary file&gt; &quot;,true);
  16. //concatStreams.mergeUsingSequenctInputStream(input1,input2); // uncomment it to run the code with sequenceInputStream
  17. concatStreams.mergeUsingFileOutputAppend(output, input2);
  18. } catch (FileNotFoundException e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. }
  22. }
  23. private void mergeUsingFileOutputAppend(OutputStream outStream, InputStream input) {
  24. try {
  25. byte[] buffer = new byte[256];
  26. int data;
  27. data = input.read(buffer);
  28. while(data != -1){
  29. String str = new String(buffer, &quot;UTF-8&quot;);
  30. //System.out.println(str);
  31. outStream.write(buffer);
  32. data = input.read(buffer);
  33. }
  34. //output.write(data);
  35. } catch (IOException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. } finally {
  39. try {
  40. outStream.close();
  41. } catch (IOException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. private void mergeUsingSequenctInputStream(InputStream input1, InputStream input2) {
  48. SequenceInputStream sequenceInputStream =
  49. new SequenceInputStream(input1, input2);
  50. FileOutputStream fos = null;
  51. try {
  52. fos = new FileOutputStream(&quot;&lt;path to first binary file&gt;&quot;);
  53. byte[] buffer = new byte[256];
  54. int data;
  55. data = sequenceInputStream.read(buffer);
  56. while(data != -1){
  57. String str = new String(buffer, &quot;UTF-8&quot;);
  58. //System.out.println(str);
  59. fos.write(buffer);
  60. data = sequenceInputStream.read(buffer);
  61. }
  62. } catch (IOException e) {
  63. // TODO Auto-generated catch block
  64. e.printStackTrace();
  65. } finally {
  66. try {
  67. fos.close();
  68. } catch (IOException e) {
  69. // TODO Auto-generated catch block
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74. }

Best Regards,
Saurav

答案1

得分: 1

你的实现中有一个bug:你总是将固定大小的字节块(等于缓冲区大小)写入输出流,请注意这行代码:

  1. outStream.write(buffer);

请考虑到输入流可能存在小于缓冲区大小的余数。
修复应该使用以下行:

  1. outStream.write(buffer, 0, data);

我已经像这样更新了你的代码,并成功合并了一个分为两部分的JPEG文件。

英文:

There is a bug in your implementation: you always write fixed chunks of bytes (equal to the size of buffer) to output stream, please take attention to this line:

  1. outStream.write(buffer);

Please take into account that input stream might have a remainder that is less than buffer size.
The fix should be using the following line:

  1. outStream.write(buffer, 0, data);

I've updated your code like this and was able to join jpeg file that was split into 2 parts.

huangapple
  • 本文由 发表于 2020年7月22日 01:36:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63020017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定