写了一段代码 大体是 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. Kali Linux 新手折腾笔记

    http://defcon.cn/1618.html 2014年09月29日 渗透测试 暂无评论 阅读 55,052 次   最近在折腾Kali Linux 顺便做一简单整理,至于安装就不再多扯了,估 ...

  2. Doubles water!!!!!!只会水题怎么破

    Doubles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  3. 【Xamarin挖墙脚系列:Xamarin正式发布了IOS的模拟器在Windows下】

    xamarin 的发展越来越迅速.如果还感觉这玩意儿是个鸡肋,辣么请跟的上时代吧 . (额,对微软产品有严重偏见的请绕行..............其实你可以看看.net 基金会现有的开源项目再说不开 ...

  4. __declspec,__cdecl,__stdcall都是什么意思?有什么作用?

    __cdecl和__stdcall都是函数调用规范(还有一个__fastcall),规定了参数出入栈的顺序和方法,如果只用VC编程的话可以不用关心,但是要在C++和Pascal等其他语言通信的时候就要 ...

  5. 顶尖数据挖掘辅助教学套件(TipDM-T6)产品白皮书

          顶尖数据挖掘辅助教学套件 (TipDM-T6)           产  品  说  明  书 广州泰迪智能科技有限公司 版权所有 地址: 广州市经济技术开发区科学城232号 网址: ht ...

  6. Unity doesn't load, no Launcher, no Dash appears

    1. 重新安装 ubuntu-desktop不起作用. Enter the following commands:- Ctrl+Alt+F1 login there by user name and ...

  7. word2vec 入门(三)模型介绍

    两种模型,两种方法 模型:CBOW和Skip-Gram 方法:Hierarchical Softmax和Negative Sampling CBOW模型Hierarchical Softmax方法 C ...

  8. MediaInfo使用简介(新版本支持HEVC)

    MediaInfo 用来分析视频和音频文件的编码和内容信息,是一款是自由软件 (免费使用.免费获得源代码).他除了提供DLL之外,本身也提供GUI工具用于查看视频信息.我使用中发现,新版本的Media ...

  9. netty demo

    Netty 4.0 demo   netty是一个异步,事件驱动的网络编程框架&工具,使用netty,可以快速开发从可维护,高性能的协议服务和客户端应用.是一个继mina之后,一个非常受欢迎的 ...

  10. Linq to sql 实现多条件的动态查询(方法一)

    /// <summary> /// Linq to sql 多字段动态查询 /// </summary> /// <returns></returns> ...