InputStream读取文件到string后OutputStream到文件,按String和Bytes拷贝

写了一段代码 大体是 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格式的文档由问题吗? 
Java代码   收藏代码
  1. public static boolean copy() {  
  2.         try {  
  3.             OutputStream os = new FileOutputStream("C://test.pdf"); // 输出流  
  4.             InputStream fis = new FileInputStream("d://test.pdf"); // 输入流  
  5.   
  6.             byte[] buf = new byte[255];  
  7.             int len = 0;  
  8.             while ((len = fis.read(buf)) != -1) {  
  9.                 os.write(buf, 0, len);  
  10.             }  
  11.   
  12.             fis.close();  
  13.             os.flush();  
  14.             os.close();  
  15.   
  16.             return true;  
  17.         } catch (FileNotFoundException e) {  
  18.             // TODO Auto-generated catch block  
  19.             e.printStackTrace();  
  20.             return false;  
  21.         } catch (IOException e) {  
  22.             // TODO Auto-generated catch block  
  23.             e.printStackTrace();  
  24.             return false;  
  25.         }  
  26.     }  


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

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

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



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

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

00

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

2014年3月28日 13:43
 
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
 
00

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

2011年9月22日 08:21
 
00

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

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


这个是按字节拷贝的,貌似没什么问题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值