写了一段代码 大体是 InputStream读取文件到string后OutputStream到文件 
遇到的问题为TXT文件大小格式等都没有问题,但是PDF\RAR等格式的就无法打开了,重新生成的文件大小会比原文件小,代码如下。 
package com.stream;

import java.io.BufferedReader; 
import java.io.ByteArrayOutputStream; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.Reader; 
import java.util.Arrays;

public class bytearry { 
// public static void main(String[] args) throws Exception{ 
//        String s = "中国"; 
//        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
//        DataOutputStream dos = new DataOutputStream(baos); 
//        dos.writeUTF(s);     
//        byte[] b = baos.toByteArray(); 
//        for(int i=0;i<b.length;i++){ 
//            System.out.println(Integer.toHexString(b[i])); 
//        } 
//        System.out.println(">>>"+new String(b,"UTF-8")+"<<<");        
//        System.out.println("--------------------"); 
//        byte[] b2 = s.getBytes("UTF-8"); 
//        for(int i=0;i<b2.length;i++){ 
//            System.out.println(Integer.toHexString(b2[i])); 
//        } 
//    }

// public static void main(String[] args) throws IOException { 
//   String str = "Hello world!"; 
//   // string转byte 
//   byte[] bs = str.getBytes(); 
//   System.out.println(Arrays.toString(bs)); 
//   
//   // byte转string 
//   String str2 = new String(bs); 
//   System.out.println(str2); 
//   
//   OutputStream os = new FileOutputStream("C://testCopy11.pdf");  //输出流 
//   FileInputStream fis = new FileInputStream("d://test.pdf");  //输入流 
//   //InputStreamReader     
//   Reader  inputStreamReader = new InputStreamReader(fis); 
//   BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
//   
//   String ss = new String();    
//   String s;    
//   while((s = bufferedReader.readLine())!=null){    
//           ss += s;    
//   } 
//   //System.out.println(ss); 
//   
//   byte[] buf = new byte[255]; 
//   
//   buf = ss.getBytes(); 
//   
//   
//
// 
//     int len = 0;  
//     //while ((len = buf.length) != -1) {  
//     // os.write(buf, 0, len); 
//     os.write(buf); 
//    // }  
//   
//     fis.close();  
//     os.flush();  
//     os.close(); 
//     //copy(); 
// 
// }

public static void main(String[] args) throws IOException { 
  String str = "Hello world!"; 
  // string转byte 
  byte[] bs = str.getBytes(); 
  System.out.println(Arrays.toString(bs)); 
  
  // byte转string 
  String str2 = new String(bs); 
  System.out.println(str2); 
  
  OutputStream os = new FileOutputStream("C://bytearry.java");  //输出流 
  FileInputStream fis = new FileInputStream("d://bytearry.java");  //输入流 
  //InputStreamReader     
  Reader  inputStreamReader = new InputStreamReader(fis); 
  BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
  
  String ss = new String();    
  String s;    
  while((s = bufferedReader.readLine())!=null){    
          ss += s;    
  } 
  System.out.println(ss.length()); 
  
  byte[] buf = new byte[255]; 
  
  buf = loadAFileToStringDE2(new File("d://bytearry.java")).getBytes(); 
  int len = 0;  
    //while ((len = buf.length) != -1) {  
    // os.write(buf, 0, len); 
    os.write(buf); 
   // }   
    fis.close();  
    os.flush();  
    os.close(); 
    //copy(); 
    System.out.println(loadAFileToStringDE2(new File("d://bytearry.java")));

}
   public static String loadAFileToStringDE2(File f) throws IOException {  
        long beginTime = System.currentTimeMillis(); 
        InputStream is = null; 
        String ret = null; 
        try { 
            is =  new FileInputStream(f) ; 
            long contentLength = f.length(); 
            byte[] ba = new byte[(int)contentLength]; 
            is.read(ba); 
            ret = new String(ba); 
        } finally { 
            if(is!=null) {try{is.close();} catch(Exception e){} } 
        } 
        long endTime = System.currentTimeMillis(); 
        System.out.println("方法2用时"+ (endTime-beginTime) + "ms"); 
        return ret;        
    } 
   
public static boolean copy() {  
   try {  
    OutputStream os = new FileOutputStream("C://test.pdf");  //输出流 
    InputStream fis = new FileInputStream("d://test.pdf");  //输入流 
  
    byte[] buf = new byte[255];  
    int len = 0;  
    while ((len = fis.read(buf)) != -1) {  
    os.write(buf, 0, len);  
    }  
  
    fis.close();  
    os.flush();  
    os.close();  
  
    return true;  
   } catch (FileNotFoundException e) {  
    // TODO Auto-generated catch block   
    e.printStackTrace();  
    return false;  
   } catch (IOException e) {  
    // TODO Auto-generated catch block   
    e.printStackTrace();  
    return false;  
   }  
}  
}


问题补充:

chen_yongkai 写道
好乱啊,是指这段拷贝pdf格式的文档由问题吗?

  1. public static boolean copy() {
  2. try {
  3. OutputStream os = new FileOutputStream("C://test.pdf"); // 输出流
  4. InputStream fis = new FileInputStream("d://test.pdf"); // 输入流
  5. byte[] buf = new byte[255];
  6. int len = 0;
  7. while ((len = fis.read(buf)) != -1) {
  8. os.write(buf, 0, len);
  9. }
  10. fis.close();
  11. os.flush();
  12. os.close();
  13. return true;
  14. } catch (FileNotFoundException e) {
  15. // TODO Auto-generated catch block
  16. e.printStackTrace();
  17. return false;
  18. } catch (IOException e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. return false;
  22. }
  23. }

这个是按字节拷贝的,貌似没什么问题

--------------------------------------------------------- 
这段是没有问题的,但是 InputStream读取文件到string后OutputStream到文件,中间多了一个string过程。


问题补充:

chen_yongkai 写道
有问题的代码呢?重点贴出,不要混在一起

代码是可以运行的吧,下面的行就是InputStream读取文件到string 到byte[]啊。 
buf = loadAFileToStringDE2(new File("d://bytearry.java")).getBytes(); 

2011年9月21日 12:30

dongni110 
20 
0 0 0
 
 
 

4个答案按时间排序按投票排序

00

请参见http://blog.csdn.net/maya2000/article/details/22394933 
inputstream 转 string 转 outputstream

2014年3月28日 13:43
aqkf-2001 
30 
0 0 0
 
00

找到问题了: 
loadAFileToStringDE2方法里 
byte[] ba = new byte[(int)contentLength]; 
            is.read(ba); 
            ret = new String(ba); //这里用的是平台默认编码

调用处: 
buf = loadAFileToStringDE2(new File("d://bytearry.java")).getBytes();//用的也是平台默认编码

应该改为: 
ret = new String(ba,"ISO8859-1");

buf = loadAFileToStringDE2(new File("d://bytearry.java")).getBytes("ISO8859-1");

平台默认编码有可能不包括某些字符,因而丢失了数据

2011年9月26日 09:40
chen_yongkai 
1600 
1 1 23
 
00

有问题的代码呢?重点贴出,不要混在一起

2011年9月22日 08:21
chen_yongkai 
1600 
1 1 23
 
00

好乱啊,是指这段拷贝pdf格式的文档由问题吗?

  1. public static boolean copy() {
  2. try {
  3. OutputStream os = new FileOutputStream("C://test.pdf"); // 输出流
  4. InputStream fis = new FileInputStream("d://test.pdf"); // 输入流
  5. byte[] buf = new byte[255];
  6. int len = 0;
  7. while ((len = fis.read(buf)) != -1) {
  8. os.write(buf, 0, len);
  9. }
  10. fis.close();
  11. os.flush();
  12. os.close();
  13. return true;
  14. } catch (FileNotFoundException e) {
  15. // TODO Auto-generated catch block
  16. e.printStackTrace();
  17. return false;
  18. } catch (IOException e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. return false;
  22. }
  23. }

这个是按字节拷贝的,貌似没什么问题

InputStream读取文件到string后OutputStream到文件,按String和Bytes拷贝的更多相关文章

  1. python 压缩文件为zip后删除原文件

    压缩.log 文件为zip后删除原文件 需要注意:本人作为小白,该脚本需要和.log在一起,后面有时间需要改正. #!/usr/local/python/bin/python #-*-coding=u ...

  2. IDEA中部署tomcat,运行JSP文件,编译后的JSP文件存放地点总结

    首先保证你正常部署了Tomcat,并且正常在浏览器中运行了JSP文件. 那么Tomcat编译后的JSP文件(_jsp.class 和 _jsp.java)的存放地点: (一)一般存放在你安装的Tomc ...

  3. Itext读取PDF模板文件渲染数据后创建新文件

    Maven导入依赖 <properties> <itextpdf.version>5.5.0</itextpdf.version> <itext-asian. ...

  4. Linux文件被删除后恢复

    当ext4中的文件被删除后,进行文件恢复:http://www.360doc.com/content/18/0320/08/51898798_738625260.shtml上面的博客是恢复删除的文件, ...

  5. Android下使用InputStream读取文件

    在Android下使用InputStream读取文件. 如果不是从头开始读取文件,使用skip 后 在读取文件 使用read读取的长度为-1会获取不到数据. 换成RandomAccessFile 使用 ...

  6. 关于AysncController的一次测试(url重写后静态页文件内容的读取是否需要使用异步?)

    因为做网站的静态页缓存,所以做了这个测试 MVC项目 准备了4个Action,分两组,一组是读取本地磁盘的一个html页面文件,一组是延时2秒 public class TestController ...

  7. C# 中使用Image.FromFile(string path)后,提示该文件正在被另一进程使用XXX的问题

    C# 中使用Image.FromFile(string path)后,提示该文件正在被另一进程使用XXX的问题 C# 中使用Image.FromFile(string path)后,提示该文件正在被另 ...

  8. [Head First Python]4.读取文件datafile.txt, 去除两边空格, 存储到列表,从列表格式化(nester.py)后输出到文件man.out,other.out

    datafile.txt  #文件 Man: this is the right room for an argument. Other Man: I've told you once. Man: N ...

  9. weblogic对JSP预编译、weblogic读取JSP编译后的class文件、ant中weblogic.jspc预编译JSP

    我们都知道在weblogic中JSP是每次第一次访问的时候才会编译,这就造成第一次访问某个JSP的时候性能下降,有时候我们也希望JSP被编译成class然后打包在jar中实现隐藏JSP的功能,下面介绍 ...

随机推荐

  1. Nand flash 的发展和eMMC

    讨论到eMMC的发展历程,必须要从介绍Flash的历史开始 Flash分为两种规格:NOR Flash和NAND Flash,两者均为非易失性闪存模块. 1988年,Intel首次发出NOR flas ...

  2. C#调用API函数EnumWindows枚举窗口的方法

    原文 http://blog.csdn.net/dengta_snowwhite/article/details/6067928 与C++不同,C#调用API函数需要引入.dll文件,步骤如下: 1. ...

  3. 【Leetcode】二叉树层遍历算法

    需求: 以层遍历一棵二叉树,二叉树的结点结构如下 struct tree_node{ struct tree_node *lc; struct tree_node *rc; int data; }; ...

  4. linux之SQL语句简明教程---主键,外来键

    主键 (Primary Key) 中的每一笔资料都是表格中的唯一值.换言之,它是用来独一无二地确认一个表格中的每一行资料.主键可以是原本资料内的一个栏位,或是一个人造栏位 (与原本资料没有关系的栏位) ...

  5. windows Oracle DBases auto backUp

  6. Linux主机规划与磁盘分区

    各硬件设备在Linux中的文件名 在Linux系统当中,几乎所有的硬件设备文件都在/dev这个目录内. 各硬件设备在Linux中的文件名: 设备 设备在Linux中的文件名 IDE接口的硬盘 /dev ...

  7. hdu 1757 A Simple Math Problem(矩阵快速幂乘法)

    Problem Description Lele now is thinking about a simple function f(x). If x < f(x) = x. If x > ...

  8. Apache启用GZIP压缩网页传输方法

    一.gzip介绍 Gzip是一种流行的文件压缩算法,如今的应用十分广泛,尤其是在Linux平台.当应用Gzip压缩到一个纯文本文件时,效果是很明显的,大约能够降低70%以上的文件大小.这取决于文件里的 ...

  9. J2EE学习的一部分--JDBC详细说明

    今天是关于我们JDBC相关知识,左右JDBC我想大家都很熟悉的,我记得在很早以前就开始使用它,我记得那是一个大二的学生做课程设计.但随后以完成任务,所以遇到的问题google,当时没有时间组织,下关于 ...

  10. jQuery滑动选取数值范围插件

    HTML 首先载入jQuery库文件以及jRange相关的css文件:jquery.range.css和插件:jquery.range.js <script src="jquery.j ...